Reputation: 117
I am trying to generate logs using log4j. Logging part is working correctly, but I want to include the hostname (where the logs are generated) to be appended in the filename.
for eg: my current logfile name: logger.2019-06-12-06-14
what i want: logger_$HOST.2019-06-12-06-14 , where $HOST is the hostname
I have already seen other posts in stackoverflow and depending on that I have set the hostname in my Java code where I think its getting set before calling log4j
static {
try {
HOST_NAME = InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException e) {
HOST_NAME = "localhost";
}
System.setProperty("hostname", HOST_NAME);
}
and have modified the cfg file (where we have properties defined for all the loggers), to include the system property:
*.*.log4j.appender.MyLogger.File=/logs/logger_$hostname;
But the resultant filename generated is logger_$hostname.2019-06-12-06-14 (which is literally $hostname instead of the property value)
In the posts, it does mention that the way to assign the variable in the cfg file is logger_${hostname} but while compiling it throws err with unexpected {, so I just put $hostname instead.
I have tried other ways too, like putting
*.*.log4j.appender.MyLogger.File=/logs/logger_$env:hostname;
*.*.log4j.appender.MyLogger.File=$hostname;
*.*.log4j.appender.MyLogger.File=/logs/logger_$HOST;
(thinking since its running in unix, it might pick the HOST variable :( )
but no luck so far. Any ideas what I might be doing wrong?
BTW, I am calling the logger by this:
private static Logger logger = Logger.getLogger("MyLogger");
So, is it possible to set the hostname from the class where I am logging. I do want to rotate my logs every minute, so dont want to mess with other log4j settings. My complete log4j appender properties:
*.*.log4j.logger.myLogger=(INFO,MyLogger);
*.*.log4j.additivity.myLogger=false; # this logger does not show up in main logs
*.*.log4j.appender.MyLogger.File=/logs/logger_$hostname;
*.*.log4j.appender.MyLogger.layout=org.apache.log4j.PatternLayout;
# %n: new line for each entry
*.*.log4j.appender.MyLogger.layout.ConversionPattern="%m%n";
*.*.log4j.appender.MyLogger.Append=true;
*.*.log4j.appender.MyLogger.ImmediateFlush=true;
*.*.log4j.appender.MyLogger.Encoding=UTF8;
# Enable 1 minute rotation
*.*.log4j.appender.MyLogger.DatePattern="'.'yyyy-MM-dd-HH-mm";
# Disable periodic flush since files will be flushed upon rotation that happens every minute
*.*.log4j.appender.MyLogger.PeriodicFlush=false;
Any ideas to fix it will be very helpful. (And apologies since its kind of a repetitive question but I am not able to figure out the issue with the earlier posts :( )
Upvotes: 0
Views: 3100
Reputation: 39
I have replaced ${hostname} by ${hostName} and it worked. <RollingFile name="LogToFile" fileName="${env:LOG_ROOT}/${env:LOG_RELEASE}/myFile.${hostName}.json" in log4j2.xml
Upvotes: 0
Reputation: 928
You can use the ${env:HOST}
option in the log4j2.xml for environment lookup as mentioned in the documentation: https://logging.apache.org/log4j/2.0/manual/lookups.html#EnvironmentLookup
For example:
<RollingRandomAccessFile name="CustomLoggerRandomFile"
fileName="logs/${env:HOST}_system.log"
filePattern="${env:HOST}_system-%d{yyyy-MM-dd}-%i.log.gz"
immediateFlush="false">
EDIT: I assumed that you are using log4j2 and the $HOST variable must be created in the host machine (where the logs are generated) as an environment variable.
Upvotes: 1