I have created a Logger over the log4j logger with ConsoleAppender.
The code is as follows,
public class AppLogger{
static {
ConsoleAppender ca = new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));
org.apache.log4j.Logger.getRootLogger().addAppender(ca);
}
public AppLogger(Class classname) {
logger = LoggerFactory.getLogger(classname);
}
public void debug(String message){
logger.debug(message);
}
...
...
}
I have not used the log4j.properties file because I am not sure where to put it. The above code is working fine and logging properly on the console. Now I want to change it to use it with a FileAppender.
I simply updated the static constructors as follows,
FileAppender fa = new FileAppender();
fa.setFile("d:/log.txt");
fa.setLayout(new PatternLayout(PatternLayout.DEFAULT_CONVERSION_PATTERN));
org.apache.log4j.Logger.getRootLogger().addAppender(fa);
And added fa insted of ca. But now I am getting this error,
log4j:ERROR No output stream or file set for the appender named [null].
I am not sure which property I am missing to define fro the File Appender.
You forgot to activate the options:
FileAppender fa = new FileAppender();
fa.setFile("d:/log.txt");
fa.setLayout(new PatternLayout(PatternLayout.DEFAULT_CONVERSION_PATTERN));
fa.activateOptions(); \\ ← activate the options
org.apache.log4j.Logger.getRootLogger().addAppender(fa);
The method OptionHandler#activateOptions() activate the options that were previously set with calls to option setters.
Related
I am facing an issue with configuring log4j2 logs programmatically.
Please see the below code, I am creating 2 objects of the class App3, and I want to create 2 debug log files(a log file per App3 object), thereafter each object should be able to log to corresponding log file.
But my code is logging all the logs to the second log file after 2nd log is created. Can someone help on this.
Output of the program
file Name: app3_logger1.log_debug.log
2020-06-16 16:19:31,100 DEBUG app3_logger1.log [main] Hello from Log4j 2app3_logger1
file Name: app3_logger2.log_debug.log
2020-06-16 16:19:31,211 DEBUG app3_logger2.log [main] Hello from Log4j 2app3_logger2
2020-06-16 16:19:31,216 DEBUG app3_logger2.log [main] Hello from Log4j 2app3_logger2
2020-06-16 16:19:31,216 DEBUG app3_logger1.log [main] Hello from Log4j 2app3_logger1
2020-06-16 16:19:31,216 DEBUG app3_logger1.log [main] Hello from Log4j 2app3_logger1
2020-06-16 16:19:31,217 DEBUG app3_logger2.log [main] Hello from Log4j 2app3_logger2
Java Class - you just need to add log4j2 dependencies to compile
public class App3 {
public Logger logger;
public static void main(String[] args) {
App3 app1 = new App3();
app1.initializeYourLogger("app3_logger1.log", "%d %p %c [%t] %m%n");
app1.testProg("app3_logger1");
App3 app2 = new App3();
app2.initializeYourLogger("app3_logger2.log", "%d %p %c [%t] %m%n");
app2.testProg("app3_logger2");
app2.testProg("app3_logger2");
app1.testProg("app3_logger1");
app1.testProg("app3_logger1");
app2.testProg("app3_logger2");
}
public void testProg(String s) {
logger.debug("Hello from Log4j 2" + s);
}
public void initializeYourLogger(String fileName, String pattern) {
this.logger = LogManager.getLogger(fileName);
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setStatusLevel(Level.DEBUG);
builder.setConfigurationName(fileName);
AppenderComponentBuilder componentBuilder = builder.newAppender("log", "File");
componentBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", pattern));
RootLoggerComponentBuilder rootLogger = builder.newRootLogger(Level.DEBUG);
LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout").addAttribute("pattern", pattern);
ComponentBuilder triggeringPolicy = builder.newComponent("Policies")
.addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", "10MB"));
componentBuilder = builder.newAppender("LogToRollingErrorFile", "RollingFile")
.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.DENY)
.addAttribute("level", Level.ERROR))
.addAttribute("fileName", fileName + "_error.log")
.addAttribute("filePattern", fileName + "-%d{MM-dd-yy-HH-mm-ss}_error.log").add(layoutBuilder)
.addComponent(triggeringPolicy);
builder.add(componentBuilder);
componentBuilder = builder.newAppender("LogToRollingDebugFile", "RollingFile")
.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.DENY)
.addAttribute("level", Level.DEBUG))
.addAttribute("fileName", fileName + "_debug.log")
.addAttribute("filePattern", fileName + "-%d{MM-dd-yy-HH-mm-ss}_debug.log").add(layoutBuilder)
.addComponent(triggeringPolicy);
builder.add(componentBuilder);
AppenderRefComponentBuilder rollingError = rootLogger.getBuilder().newAppenderRef("LogToRollingErrorFile");
AppenderRefComponentBuilder rollingDebug = rootLogger.getBuilder().newAppenderRef("LogToRollingDebugFile");
rootLogger.add(rollingError);
rootLogger.add(rollingDebug);
builder.add(rootLogger);
Configurator.reconfigure(builder.build());
}
}
This is exactly what I want to do in log4j older version, I am still struggling with log4j2,
private void initLogger(String serviceName, String instance) throws IOException {
String loggerName = serviceName+"_"+instance;
this.logger = Logger.getLogger(loggerName);
this.logger.removeAllAppenders();
PatternLayout layout = new PatternLayout();
layout.setConversionPattern("%d: %m%n");
String loggingFolder = this.properties.getLoggingFolder();
String debugFileName = loggingFolder+"/"+loggerName+"_debug.log";
String errorFileName = loggingFolder+"/"+loggerName+"_error.log";
RollingFileAppender debugAppender = new RollingFileAppender(layout, debugFileName, true);
debugAppender.setThreshold(Level.DEBUG);
debugAppender.setMaxFileSize("10000000");
debugAppender.setMaxBackupIndex(49);
logger.addAppender(debugAppender);
RollingFileAppender errorAppender = new RollingFileAppender(layout, errorFileName, true);
errorAppender.setThreshold(Level.ERROR);
errorAppender.setMaxFileSize("10000000");
errorAppender.setMaxBackupIndex(49);
logger.addAppender(debugAppender);
}
Actually, I am quite sure your Logger is being updated correctly. The problem is that both application instances are going to use the same Logging configuration.
If you look at Log4j's architecture you will see that the Loggers and the configuration are anchored in the LoggerContext. By default, Log4j uses the ClassLoaderContextSelector, which means that every ClassLoader can have its own LoggerContext. Your sample application only has a single ClassLoader and so will only have a single LoggerContext and, therefore, only a single Configuration.
So when you reconfigured the second time you simply replaced the prior configuration with the new one. Since the root logger is handling all log events it will route the events from both Loggers you have created to the file created in the second configuration.
If you want logs to end up in two different files then create a configuration with both files and figure out a way to route them to the correct file, either via logger names or filters.
As per observation, your logger is updated with the last initialization so after app2.initializeYourLogger() your object-level logger object updated with App2 configuration.
You need to return the logger object from initializeYourLogger() and also create separate Logger object.
i am using multiple loggers to append into multiple log files.the path of these log files are set dynamically.
i am doing my project in java and using log4j framework.
my project structure.
src
log4test
log4test.java
log4test2
log4test2.java
log4j.properties
log4test.java:-
public class Log4Test{
public static void main(String[] args)throws IOException,SQLException{
System.setProperty("logs", "C:\\New folder 3\\logs.log");
Logger log = Logger.getLogger("file");
log.info("hello");
Log4Test2.fefe();
}
}
log4test2.java:-
public class Log4Test2{
/* Get actual class name to be printed on */
public static void fefe()throws IOException,SQLException{
System.setProperty("logs.file", "C:\\New folder 2\\logs.log");
Logger log = Logger.getLogger("admin");
log.info("Debug2");
log.info("Info2");
}
}
log4j.properties:-
log4j.rootLogger=INFO
log4j.logger.file=INFO, file
log4j.logger.admin=INFO, admin
# Define the file appender
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=$[logs}
# Define the layout for file appender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss}
[%p] %m%n
# Define the file appender
log4j.appender.admin=org.apache.log4j.FileAppender
log4j.appender.admin.File=${logs.file}
# Define the layout for file appender
log4j.appender.admin.layout=org.apache.log4j.PatternLayout
log4j.appender.admin.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss}
[%p] %m%n
i am getting this error:-
log4j:ERROR setFile(null,true) call failed
Could you check this line
# Define the file appender
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=$[logs} //TODO should be ${logs}
If I am not mistaken. Thank you.
The second thought is:
public class Log4Test2{
private final Logger logger = LoggerFactory.getLogger("admin");
public void fefe()throws IOException,SQLException{ //try remove static
System.setProperty("logs.file", "C:\\New folder 2\\logs.log");
log.info("Debug2");
log.info("Info2");
}
Although I've read multiple other questions and answers regarding this issue, I didn't find the answer and would appreciate any help.
public static void rerouteAppenderToFile(File file, Logger... log) {
for (Logger logger : log) {
rerouteAppenderToFile(file, logger);
}
}
public static void rerouteAppenderToFile(File file, Logger log) {
log.setAdditivity(false);
log.setLevel(Level.DEBUG);
RollingFileAppender fa = new RollingFileAppender();
fa.setName("RollingFileAppender_" + log.getName());
fa.setMaxBackupIndex(3);
fa.setMaxFileSize("20MB");
File folder = file.getParentFile();
folder.mkdirs();
if (folder.exists() && folder.isDirectory()) {
fa.setFile(file.getAbsolutePath());
fa.setLayout(new PatternLayout(LOGGER_PATTERN));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();
log.addAppender(fa);
}
}
What I'm trying to achieve here is being able to append multiple loggers (2 or more) to the same file using a RollingFileAppender. For some reason the files aren't rolled and the logs keep getting larger
The problem is that you create multiple RollingFileAppenders that write to the same file.
So even if one RollingFileAppender tries to roll over to a new file it cannot do so, since the file is locked by the other RollingFileAppenders.
What you could do is to create only one RollingFileAppender instance and reroute the loggers to this single instance.
The following code gives the idea, but it won't work if you call rerouteAppenderToFile() several times for a single file.
public static void rerouteAppenderToFile(File file, Logger... log) {
RollingFileAppender fa = createAppender(file);
for (Logger logger : log) {
rerouteAppenderToFile(f, logger);
}
}
public static Appender createAppender(File file) {
RollingFileAppender fa = new RollingFileAppender();
fa.setName("RollingFileAppender_" + log.getName());
fa.setMaxBackupIndex(3);
fa.setMaxFileSize("20MB");
File folder = file.getParentFile();
folder.mkdirs();
fa.setFile(file.getAbsolutePath());
fa.setLayout(new PatternLayout(LOGGER_PATTERN));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();
return fa;
}
public static void rerouteAppenderToFile(Appender fa, Logger log) {
log.setAdditivity(false);
log.setLevel(Level.DEBUG);
log.addAppender(fa);
}
I have successfully created a log at each process run time. The issue I am having now is any packages that are called that are not children of the current running process does not write its log to my file. For example I create a new log file called running-.log. The process that is running is com.me.foo inside of this class there is a call to a method in com.you and another one in com.zee . I would like to have com.you and com.zee logs write to the running-.log and not to the console log. It isn't as simple as just changing the getLogger() method to be a child of com.me.foo. Some of the logs are written out from third party jars. I am at a loss. If you need to see more code or some additional info, please let me know. There has to be another way to handle this.
Thanks
Code to create the log file dynamically
public void createLogInstance(String packaging,String appenderName, String logFileName){
Logger logger = Logger.getLogger(packaging);
Appender fileAppender = logger.getAppender(appenderName);
if(fileAppender != null){
logger.removeAppender(fileAppender);
}
//Create the root appender
ConsoleAppender console = new ConsoleAppender();
String pattern = ....;
console.setLayout(new PatternLayout(pattern));
console.setThreshold(Level.FATAL);
console.activateOptions();
logger.addAppender(console);
FileAppender fa = new FileAppender();
fa.setName(appenderName);
fa.setFile(logFileName);
fa.setLayout(new PatternLayout(..));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();
logger.setAdditivity(false);
logger.addAppender(fa);
}
com.zee log
private static Logger logger = LoggerFactory.getLogger(Zee.class);
com.you log
private static Logger logger = LoggerFactory.getLogger(You.class);
I was missing the rootLogger. Changed this line
Logger logger = Logger.getLogger(packaging);
to
Logger logger = Logger.getRootLogger();
If someone has a better way please let me know.
In my application I read in an inputfile (e.g. myFile1.txt) and create an outputfile with the same name (e.g. myFile2log). Point is that the inputfile will be read within the java application and not given as command line parameter. Therefore it is required to set an appender in the application, e.g.
public class Example {
private static final Logger LOG = Logger.getLogger(Example.class);
public Example() throws IOException {
FileAppender appender = new DailyRollingFileAppender(new PatternLayout(
PatternLayout.DEFAULT_CONVERSION_PATTERN), "yourfilename.log",
"'.'yyyy-MM-dd");
LOG.addAppender(appender);
LOG.setLevel((Level) Level.DEBUG);
LOG.debug("blabla");
new RandomClass();
}
public static void main(String[] args) throws IOException {
new Example();
}
}
public class RandomClass {
private static final Logger LOG = Logger.getLogger(RandomClass.class);
public RandomClass() {
LOG.debug("hello Randomclass");
}
}
And here is the problem: if I do the above the custom file appender only works for the specific class where the fileappender was defined, but not on any other class. Therefore the output of the "RandomClass" will not be written into this logfile, but which is what is required. How can I achieve that?
If you would like to set the appender dynamically you should try setting the new appender to the root logger:
Logger logger = Logger.getRootLogger();
FileAppender appender = new DailyRollingFileAppender(new PatternLayout(
PatternLayout.DEFAULT_CONVERSION_PATTERN), "yourfilename.log", "'.'yyyy-MM-dd");
logger.addAppender(appender)
You can use log4j.properties file in your class path then simply you can initilize logger in all the classes.
private static final Logger LOG = Logger.getLogger(Example.class);
and Appender all not required in constructor. your property file should contain
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=yourfilename.log
log4j.appender.R.MaxFileSize=2048KB