1
donuts
2y

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?

Comments
  • 0
    Why not a wrapper script?
    Why not logstash/elastic?
  • 0
    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)
  • 0
    @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.
  • 1
    @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">
  • 0
    @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?
  • 0
    > 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
  • 0
    @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...
  • 0
    @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
  • 0
    @IntrusionCM didn't try sys, will try when I'm in. Or today supposedly. thanks
  • 0
    @IntrusionCM OMG!!! I worked using sys... Thanks!

    Where are the docs that go over this stuff...
  • 1
    @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. :)
  • 0
    @IntrusionCM yes guess I jumped to conclusions too early when the ${env:pid} was used literally so thought FileAppender didn't support this.
Add Comment