0

Adding Property in Scala Environment Properties

val sysProps = System.getProperties
sysProps.setProperty("current.date.time", LocalDateTime.now().toString())

i'm able to save this property.

I tried accessing this property(current.date.time) in log4j.properties like below

log4j.appender.file.File=C:/Users/vsami/Desktop/Demo_${current.date.time}.log
log4j.appender.file.File=C:/Users/vsami/Desktop/Demo_${env:current.date.time}.log

Log file is getting generated in above location like Demo_.log, Expected :- Demo_2019/11/27T13:21:00.log

Above implementation is not helping me in accessing variable from environment properties and generate log file with expected naming convention.

Venkatesh
  • 1
  • 2
  • please share the error you receive, if at all – Sarath Chandra Vema Nov 27 '19 at 12:25
  • Maybe the system property was set too late (after Log4J already initialized itself). – Thilo Nov 27 '19 at 13:59
  • JVM system properties are not the same as environment variables. I don't think you can set environment variables from within the JVM. And as @Thilo pointed out, even if you could then Log4J might have already initialized itself before your code manages to set the environment variable. – Jasper-M Nov 27 '19 at 14:03
  • @Thilo, yes you are correct - I tried setting system property before initializing log4j, it worked and file is getting generated in expected manner - Thanks for the Suggestion. – Venkatesh Nov 28 '19 at 06:04
  • @Thilo, in local i'm able to generate the logs in expected manner by initializing log4j after - when i tried to do the same in cloudera environment i'm not getting the expected result - am i missing something here ? – Venkatesh Nov 28 '19 at 07:01

1 Answers1

0

JVM has properties that can be passed via -D parameter at VM boot. -Dprop=value. These properties can be read via System.getProperties API call. See docs for more info.

Environment variables are not specified on JVM boot and managed independently from VM by your boot environment (it can be shell, bash etc). You cannot change environment variables in already running VM. These variables can be read via System.getenv()

$ is a look up operator in log4j and can be used to resolve env variables with env: prefix or Main Arguments Lookup with prefix main:.

You could use main:current.date.time and initialise your value as following

MainMapLookup.setMainArguments(Array("--current.date.time", LocalDateTime.now().toString()));

Make sure that MainMapLookup is called before logging is initialised.

Ivan Stanislavciuc
  • 7,140
  • 15
  • 18