Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Just to make sure we are on the same page....
Recent Logv4J version?
Then two possibilities exist:
#!/usr/bin/env bash
log_dir="${HOME}/log"
java -DLOG_FILE_DIR="${log_dir}" -jar <...>
https://logging.apache.org/log4j/...
Using system properties lookup.
Alternatively use the env lookup if you want to access the environment directly:
https://logging.apache.org/log4j/...
Important notice, as I forget this a thousand times and more:
${env:HOME} is the environment of the java process - make sure that the shell that actually spawned the process has the correct environment set. Adding debugging in Java ala System.getEnv() is _mandatory_ in my opinion, as you otherwise can fall into a deep pit of madness. I found once out the hard way that some spawn scripts like to e.g. fork without properly passing down the environment. Lots of madness.
System properties is a tad easier as you can see the process call with it's arguments. (if its stupid and it works it ain't stupid.... :-P) -
donuts238483y@IntrusionCM Log4j FileAppender. Want to override the fileName value at runtime so it's app_${pid}.log
But the appender doesn't evaluate it, literally outputs to that fileName.
Tried passing it as a JVM arg: -Dpid=100 to test on local. -
@donuts
It should be ${sys:<var_name>} or ${env:<var_name>}
See the two links. You're missing the prefix. Out of memory it is then e.g.
<Appender type="File" name="LogFile" fileName="log_${sys:pid}.log"> -
donuts238483y@IntrusionCM ah I tried env: but didn't work. Not sure if the condition was met though as I passed it in using VM args -Dpid=..
But with that still ended up with a log file with "${pid}".
But, if it does evaluate the expression, shouldn't it be "" if pid is never set?
I did try with env: but it wouldn't even create the log file since on Windows, can't have : in a file name which is why I suspected the FileAppender takes that as a literal, it won't replace the ${var_name} no matter what? -
> we have multiple instances of an app running but they can't be all writing to the same file
And this, kids, is why we send logs to a DB and not a file -
donuts238483y@IHateForALiving db crashes/freezes... Hangs all the jobs...
In our case used a socket server but no one has figure out why and sorta feels like overkill as it just takes everything and wires it into a single log file...
Maybe that last point is the problem... Hopefully it's using a RollingFileAppender with a Max size... -
@donuts Very weird.
I can lookup an example tomorrow, currently too tired.
Usually OS shouldn't matter. Except I'm confused - when you pass the argument with -Dpid it's a system property, so you can't take ${env:pid}. It must be ${sys:pid}.
Send me a ping tomorrow, I'll get you an full example -
donuts238483y
-
@donuts I posted the links in my first reply (3rd comment)
These are lookups. Maybe some sleep is needed? 🤣
But glad we could work that out. :) -
donuts238483y@IntrusionCM yes guess I jumped to conclusions too early when the ${env:pid} was used literally so thought FileAppender didn't support this.
Related Rants
I'm trying to create log files with the PID or some JVM arg like app name but File appender doesn't parse ${myVar} in the config.
Issue is we have multiple instances of an app running but they can't be all writing to the same file.
I tried creating a custom Appender by pretty much copying the source code of FileAppender and then adding a function to add PID to the filename.
But when I use it, get some error saying "name, and fileName" are invalid parameters.
So wondering if anyone has experience building one that works out maybe there an existing code for such an appender?
question
java
log4j2