Log4j logging to separate files - java

I have an application that listens on a specific port to do its task.
This application can run on multiple instances by specifying different
ports in the argument.
MyApp-1211.bat contains
java MyApp 1211
MyApp-1311.bat contains
java MyApp 1311
MyApp-1411.bat contains
java MyApp 1411
This application logs to a file. The problem is all three instances log
into a single file, myApp.log. Is there a way to tell log4j to use
different log files? like:
myApp-port1211.log
myApp-port1311.log
myApp-port1411.log

Of course you can. One way would be to create multiple configuration files (log4j.xml / log4j.properties) - one per port respectively process. Upon loading the configuration file you could select the correct one based on the current port number:
PropertyConfigurator.configure("log4j-" + port + ".properties");
Create the config files accordingly: log4j-1211.properties, log4j-1311.properties, ...
The alternative would be to configure the file logging at runtime via Java code:
String logFilename = "./myApp-port" + port + ".log";
Layout layout = new PatternLayout("%d{ISO8601} %-5p [%t] %c{1}: %m%n");
FileAppender fileAppender = new FileAppender(layout, logFilename, false);
fileAppender.setThreshold(Level.DEBUG);
Logger.getRootLogger().addAppender(fileAppender);

You can refer to system properties in log4j.xml as following:
<appender name="ROLL" class="org.apache.log4j.rolling.RollingFileAppender">
<!-- The active file to log to -->
<param name="file" value="mylog${MY_PARAM}.log" />
</appender>
Now you just have to insert your parameter into system properties either programmatically System.setProperty("MY_PARAM", args[0]) into your main() method or when you are running java:
java -DMY_PARAM=1234 MyApp 1234
Obiously you can avoid dupplication if you are running you application from bat or shell script like:
java -DMY_PARAM=%1 MyApp %1
Please see the following references for details:
http://wiki.apache.org/logging-log4j/Log4jXmlFormat
Using system environment variables in log4j xml configuration

If you go for loading the property configuration file yourself, as suggested by other answers, don't forget to disable the log4j default initialization by adding the env. variable definition to your program's command line:
-Dlog4j.defaultInitOverride=true

Related

How to change the path for logging.file in (logback.xml) for UAT server?

I am trying to run a project on a UAT (Linux) server, but there is a problem, I need the logs to be written on the test server in a different path.
The project resources contain logback.xml, which specifies where to write logs:
<property name = "logging.path" value = "/logs"/>
<property name = "logging.file" value = "app-logger"/>
I cannot change this file, because in this case everything is correct, but for UAT I created another logback.xml and put it not in the resources but in another directory, and when I run the jar file I want to specify that I need to take this one logback.xml
On UAT I execute this command
java -Xmx512M -jar /home/user/app/program/program-0.1.jar --logging.config = app/program/config/logback.xml
I got this error:
ERROR in ch.qos.logback.core.rolling.RollingFileAppender [RollingFile] - openFile (/logs/app-logger.log,true) call failed. java.io.FileNotFoundException: /logs/app-logger.log (No such file or directory)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration (LogbackLoggingSystem.java:169)
So the second file logback.xml which I created is ignored and the one that lies in the resources is used. What am I doing wrong? How to put another logback.xml into Spring?
You can add in your required log directory to your environment specific properties file,
should be something like application.uat.properties. So you would add in something like LOG_DIR=path/to/directory
Once that is done add in '<property resource="application.${env}.properties"/>' to your logback.xml
And finally in your file appender , add '<file>${LOG_DIR}/serive.log</file>'

How to define log4j default file location

I'm setting up "log4j" logging framework to my java project, and I want to define a default path to the log4j.appender.X.File=${file.name}configuration property, if nothing to the ${file.name} argument has been specified by the comand line.
I need the solution to be dynamic:
- when I run the .jar file using -Dfile.name=C:\log\directory\path\log4j-application.log JVM argument on the comand line --> the framework should write to the path that I've specified on the argument;
- when I run the .jar file without using the JVM argument on the comand line --> the framework should write on the .jar file path;
Where should I give the default path to make my project understand where it should write the file on?
This is a part of my actual log4j.properties file:
# Root logger option, use one of the following log levels: INFO, WARN, ERROR, DEBUG, TRACE
log4j.rootLogger=DEBUG, stdout, file
[...]
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${file.name}
log4j.appender.file.MaxFileSize=100MB
log4j.appender.file.MaxBackupIndex=3
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout=com.webratio.rtx.log.SessionAwarePattern
log4j.appender.file.layout.ConversionPattern= %d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file.Append=true
log4j.appender.file.Threshold=TRACE
With the attached settings, the app is working partly:
1. java -Dfile.name=C:\log\directory\path\log4j-application.log -jar C:\Users\martin\Desktop\Projects\Log4j\log4j_example.jar: including the JVM argument, the .jar correctly created the log file inside the already defined JVM argument path;
2. java -jar C:\Users\martin\Desktop\Projects\Log4j\log4j_example.jar: excluding the JVM argument, the .jar is not able to understand where it should write on, therefore no file has been created;
I was expecting that, on the second attempt, the app could write on the same .jar file folder, since the ${file.name} argument was not defined on the comand line.That didn't work because it seems that something is missing to assign that "default" path to write on.
I also tried setting log4j.appender.file.File=${file.name}/log4j.log on the log4j.properties file instead, giving java -Dfile.name=C:\log\directory\path\ -jar C:\Users\martin\Desktop\Projects\Log4j\log4j_example.jaras a comand and excluding the log4j.log name to the path.
Niether this attempt is working for me, since system is raising the following error:
[...]
log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: \log4j.log (Access denied)
[...]
This time I was expecting that the framework could write on the same .jar file folder, since the ${file.name} argument was not defined on the comand line and atuomatically get the root of the .jar file (expecting that's the default writing folder).
I'm using the 1.2.17 log4j version.
#in properties file put like this
log4j.appender.file.File=./history.log

How to enable/setup log4j for oozi java workflows?

I'm running an Oozie Java workflow (the jar file is in HDFS), and I'd like to add logging functionality to my application. Does anybody know how to do it? Where should I put my "log4j.properties" file? How can I make log4j to output the log to a location in HDFS?
Looking in this documentation, you can try adding oozie-log4j.properties in your oozie directory (where workflow.xml is).
Here are the default settings:
log4j.appender.oozie=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.oozie.RollingPolicy=org.apache.oozie.util.OozieRollingPolicy
log4j.appender.oozie.File=${oozie.log.dir}/oozie.log
log4j.appender.oozie.Append=true
log4j.appender.oozie.layout=org.apache.log4j.PatternLayout
log4j.appender.oozie.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
log4j.appender.oozie.RollingPolicy.FileNamePattern=${log4j.appender.oozie.File}-%d{yyyy-MM-dd-HH}
log4j.appender.oozie.RollingPolicy.MaxHistory=720
It also outlines the following restrictions:
In order for Oozie logging to work 100% correctly, the following restrictions must be observed (described below and in the oozie-log4j.properties file):
The appender that Oozie uses must be named "oozie" (i.e. log4j.appender.oozie )
log4j.appender.oozie.RollingPolicy.FileNamePattern must end with "-%d{yyyy-MM-dd-HH}.gz" or "-%d{yyyy-MM-dd-HH}". If it ends with ".gz" the old logs will be compressed when rolled
log4j.appender.oozie.RollingPolicy.FileNamePattern must start with the value of log4j.appender.oozie.File

Application specific log in tomcat 7 using JULI?

I'm using java system logging in tomcat 7, but no logging statements get written to the log. I've added this file to my WEB-INF/classes. The log file "new-xyz-test" gets created (so I have at least some of the config right) but its empty - no log statements get printed to it.
handlers=java.util.logging.ConsoleHandler, org.apache.juli.FileHandler
org.apache.juli.FileHandler.level=ALL
org.apache.juli.FileHandler.directory=${catalina.base}/logs
org.apache.juli.FileHandler.prefix=new-xyz-test-
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
com.xyz.level=ALL
com.xyz.handlers=org.apache.juli.FileHandler
To configure JULI in the web applications you need have a logging.properties file in the WEB-INF/classes directory. If you use the default handlers, you may lose messages. You need to specify a prefix for the handler in your file.
handlers=1FILE.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
.handlers=java.util.logging.ConsoleHandler
1FILE.org.apache.juli.FileHandler.level=FINEST
1FILE.org.apache.juli.FileHandler.directory=/app-logs
1FILE.org.apache.juli.FileHandler.prefix=file-1
java.util.logging.ConsoleHandler.level=FINE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
com.xyz.level=INFO
com.xyz.handlers=1FILE.org.apache.juli.FileHandler
com.abc.level=INFO
com.abc.handlers=java.util.logging.ConsoleHandler
A
handler prefix (e.g. 1FILE.) starts with a number, then has an arbitrary string, and ends with a period (.).
See more in Logging in Tomcat
Arguments in the JVM
If you are not running the Tomcat from the startup.sh or startup.bat, you need to specify:
The location of the general logging.properties for Tomcat (in the conf directory of Tomcat)
The manager org.apache.juli.ClassLoaderLogManager. This is important because allows you to configure
for each web application different loggin options. By default, a JVM process can only have a single configuration file.) ,
Similar to the next (I'm using eclipse):
-Djava.util.logging.config.file="C:\Users\Paul\workspaces\utils\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\logging.properties" -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
By default, java.util.logging read the file that is included in the JDK or JRE, e.g.:
"C:\Software\jdk1.7.0_17\jre\lib\logging.properties"
Setting Tomcat Heap Size (JVM Heap) in Eclipse, for how to add arguments in the VM
are you sure that you write to the correct logger , i.e. Logger.getLogger("com.xyz")?
I think that you may got wrong when you wrote in logging.properties:com.xyz.level=ALL com.xyz.handlers=org.apache.juli.FileHandler in the case that you actually write to the logger Logger.getLogger(com.xyz.YourClass.class), that because in the logging properties file you should write the logger name which is in your case com.xyz.YourClass

log4j file appender not working

am using log4j-1.2.15.jar for enable logging .and its writing all logs to a file.
this is what in my log4j.properties.
log4j.rootLogger = DEBUG, fileout
log4j.appender.fileout = log.NewLogForEachRunFileAppender
log4j.appender.fileout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c - %m%n
log4j.appender.fileout.layout = org.apache.log4j.PatternLayout
log4j.appender.fileout.File = D:/log/logs.log
It was working fine when am trying to run this from my local server configured in eclipse.
But the same is not working when i had deployed that into the production development enviornment.This is what am getting in the console.
no output stream or file set for the appender named [fileout]
Can anyone give a solution.?
Your configuration looks ok. I assume the D:/log/logs.log is available in production environment.
You might want to try log4j configuration debugging by setting -Dlog4j.debug on the command line. It often points out useful configuration errors.

Categories