How to have log4j.properties read env variables - properties

This should just be a quick question. I am wondering if this syntax would be correct.
Inside log4j.properties
log4j.rootLogger=${ROOT_LOGGER:INFO}, console
So what I'm trying to achieve is to have a ROOT_LOGGER env variable and if it's not present, fallback to INFO. I know this works in .yaml files, so just wondering if the same applies here.

Long time but Im going to put my found solution here.
we can add vars on properties like:
${sys:some.property:-default_value}
${env:ENV_NAME:-default_value}
build.gragle dependencies
dependencies {
implementation "org.apache.logging.log4j:log4j-api:2.14.1"
implementation "org.apache.logging.log4j:log4j-core:2.14.1"
}
log4j2.properties
appender.consoledev.type=Console
appender.consoledev.name=CONSOLEDEV
appender.consoledev.layout.type=PatternLayout
appender.consoledev.layout.pattern=%d{HH:mm:ss.sss} %5p %20logger{36} : %msg%n
loggers=logapp
logger.logapp.name=${sys:logging.package:-com.test}
logger.logapp.level=${sys:logging.level:-DEBUG}
logger.logapp.additivity=false
logger.logapp.appenderRef.consoledev.ref=CONSOLEDEV
running jar with args
java -jar -Dlogging.level=INFO -Dlogging.package=com.test.MainClass test.jar
java -jar -Dlogging.level=INFO -Dlogging.package=com.test test.jar
java -jar -Dlogging.level=DEBUG -Dlogging.package=root test.jar

Related

Using custom java class in karate feature file [duplicate]

Good morning. I am working on a project that uses Karate Standalone. I am completely new to Karate to excuse my lack of knowledge here.
The standalone karate jar is executed with the '-m' command line parameter to start a mock.feature. The mock.feature references a utils class that is built on 'org.springframework.amqp'.
The problem is that the karate.jar startup fails with a Command Line Execution Exception due to external library 'org/springframework/amqp/rabbit/connection/ConnectionFactory'
api1_mock_test.feature
Feature: API1 Mock Test
Background:
* def RabbitUtils = Java.type('utils.RabbitUtils')
.
.
Our RabbitUtils is just a java class that imports org.springframework.amqp external libraries to provide functions to interact with a Rabbit AMQP broker e.g. connect, receive, publish, purge etc. When built and run in IntelliJ all works ok. The POM reference in the project is:
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
Does the Karate standalone jar have some way of referencing external libraries? The classpath parameter is set to reference our workspace '.\target\test-classes' and contains the RabbitUtils.class file.
The current execution from workspace root looks like this:
java -jar C:\intuit\karate-0.9.3.RC2.jar -cp .\target\test-classes -p 6868 -m .\src\test\java\mocks\api1_mock_test.feature
08:57:05.122 [main] INFO com.intuit.karate.Main - Karate version: 0.9.3.RC2
08:57:05.891 [main] ERROR com.intuit.karate - server-side background init failed - api1_mock_test.feature:4
Exception in thread "main" picocli.CommandLine$ExecutionException:
-unknown-:4 - org/springframework/amqp/rabbit/connection/ConnectionFactory
Thank you!
Thanks for asking this, and I think I've figured out a way to do this which opens up a lot of great possibilities. The solution is to use Java first-principles, and not use the -jar option. The Karate command-line-app (or CLI) class happens to be com.intuit.karate.Main. I'm going to provide a demo here of using SikuliX. First, the feature file test.feature:
Feature: sikuli test
Background:
* def Screen = Java.type('org.sikuli.script.Screen')
Scenario:
* def s = new Screen()
* def c = s.capture()
* c.getFile('.')
And with the karate.jar and sikulixapi.jar in the same folder on the command line, this works (for windows, use ; instead of : as the "path separator"):
java -cp karate.jar:sikulixapi.jar com.intuit.karate.Main test.feature
For those looking to customize the classpath for the Visual Studio Code "Karate Runner" extension, please refer this: https://github.com/intuit/karate/wiki/Karate-Robot-Windows-Install-Guide#change-command-line-settings
Also see: https://stackoverflow.com/a/58398958/143475
For those who really don't want to compile Java but need to use some JVM libraries, it is possible via pure JS, (but hard to troubleshoot and debug): https://stackoverflow.com/a/65035825/143475

How to make Karate Tests run as a service [duplicate]

I am using karate 0.9.2 with gradle. My project requires to have all karate tests inside src/main/java. So I configured the gradle dependency as ‘compile’ instead of ‘testCompile’ and also modified the sourceSets to point to main instead of test. When I ran my runner class with above configuration I got empty test suite message.
build.gradle snippet:
compile 'com.intuit.karate:karate-junit4:0.9.3'
compile 'com.intuit.karate:karate-apache:0.9.3'
sourceSets {
test {
resources {
srcDir file('src/main/java')
exclude '**/*.java'
}
}
}
Additionally, I have is to run the karate tests from the deployable project jar. Please point me the resources I can refer to achieve the same.
Not something we directly support but teams have done this in Spring Boot etc. It should be possible, see if this thread helps: https://github.com/intuit/karate/issues/520
Also you may not need JUnit also: https://github.com/intuit/karate/issues/427
And see the sample project in this ticket as an example: https://github.com/intuit/karate/issues/529
EDIT - in 1.0 onwards we hope that class-loading from spring-boot JAR files is more reliable: https://github.com/intuit/karate/issues/751

Runner.runFeature can't find the feature file path - karate 0.9.9.RC3-junit5 [duplicate]

I am using karate 0.9.2 with gradle. My project requires to have all karate tests inside src/main/java. So I configured the gradle dependency as ‘compile’ instead of ‘testCompile’ and also modified the sourceSets to point to main instead of test. When I ran my runner class with above configuration I got empty test suite message.
build.gradle snippet:
compile 'com.intuit.karate:karate-junit4:0.9.3'
compile 'com.intuit.karate:karate-apache:0.9.3'
sourceSets {
test {
resources {
srcDir file('src/main/java')
exclude '**/*.java'
}
}
}
Additionally, I have is to run the karate tests from the deployable project jar. Please point me the resources I can refer to achieve the same.
Not something we directly support but teams have done this in Spring Boot etc. It should be possible, see if this thread helps: https://github.com/intuit/karate/issues/520
Also you may not need JUnit also: https://github.com/intuit/karate/issues/427
And see the sample project in this ticket as an example: https://github.com/intuit/karate/issues/529
EDIT - in 1.0 onwards we hope that class-loading from spring-boot JAR files is more reliable: https://github.com/intuit/karate/issues/751

How to run main function Kotlin file on command-line with string array arguments

i'm trying to run one of .kt files that i have for my side project and I suddenly wanted run my *.kt file that has
fun main(args: Array<String>) {
...
}
on command-line tool. I did install kotlin compiler, set up the path. All I have to do is to figure out how to pass args parameter through command line and cannot find a way to do that.
I've looked through kotlin compiler options and how to run kotlin file on command-line and it did not help at all.
Thanks in advance.
Just type them after the command:
java -jar myjar.jar arg_one arg_two
//OR (After compiling to jars you can specify the main class to run)
kotlin -classpath myjar.jar MyKt 'arg_one' 'arg_two'
//OR
kotlin MyKt 'arg_one' 'arg_two'
Then:
val argOne = args[0] // "arg_one"
First you have to compile your file to a JAR:
kotlinc filename.kt -include-runtime -d output.jar
Then you can run that JAR with java:
java -jar output.jar argument0 argument1

loading spring-{env}.properties in IntelliJ 2016.3

I have a Spring Boot properties with three properties files:
I have a class that uses values defined in application-local.properties file, which I thought would get used because application-properties has `spring.profiles.active=local
When I start up in IntelliJ, I see this in the console logs:
Loaded config file 'file:.../build/resources/main/application.properties' (classpath:/application.properties)
2016-12-18 20:10:17.596 DEBUG 29999 --- [ restartedMain] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'file:.../build/resources/main/application-local.properties' (classpath:/application-local.properties)`
Skipped (empty) config file 'file:...build/resources/main/application-local.properties' (classpath:/application-local.properties) for profile local
It says it skipped the empty config file, but when I inspect 'build/resources/main/application-local.properties' then I see the values in there.
However, the following class does not have the properties when I run it from within IntelliJ as a Spring Boot Application. It throws a null pointer because these are null:
#Value("${myapp.ApiKey}")
private String apiKey;
#Value("${myapp.ApiSecret}")
private String apiSecret;
#Value("${myapp.user}")
private String user;
In the Application Configuration I tried using Active Profiles, Program Arguments, and Environment Variables.
However, if I run from the command line as:
java -jar -Dspring.active.profiles=local build/libs/myjar-0.0.1-SNAPSHOT.jar
then it works just fine. I am sure this was working previously, but not sure if the IntelliJ version was the same. Any tips on how to make the correct version of my application.properties get used?
Which spring boot version are you using? If you're using the latest this should be
-Dspring.profiles.active=local
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles