Tried other question answers but couldn't overcome the problem
I have xml file for configure the log4j. Two log files are created but files are empty. I cannot write to a file.
This is my xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration
xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="ErrorAPPENDER"
class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="D://Error.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd,HH:mm:ss},%m%n" />
</layout>
</appender>
<appender name="EventAPPENDER"
class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="D://Event.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd,HH:mm:ss},%m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
</appender>
<logger name="Event" additivity="false">
<level value="debug" />
<appender-ref ref="EventAPPENDER" />
</logger>
<logger name="Error" additivity="false">
<level value="debug" />
<appender-ref ref="ErrorAPPENDER" />
</logger>
<root>
<priority value="debug" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4j:configuration>
And this is the initialize class
package com.mobios.common;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Log4JUtil {
public static final Logger logger= Logger.getLogger(Log4JUtil.class);
}
And this is how i logging
Log4JUtil.logger.getLogger("ErrorAPPENDER").error("sdsffd");
But nothing writes on the log file. I cannot see any error and i tried the solutions mentioned in other questions.
A logger is always identified by a unique name across all the loggers initialized until now
Logging.getLogger(Log4jUtil.class) creates a logger with name "com.mobios.common.Log4jUtil" (Log4jUtil.class.getName()) or returns an already created logger with that name
https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Logger.html#getLogger(java.lang.Class)
In your log4j.xml file, you have not created any logger with the name "com.mobios.common.Log4jUtil". You created loggers with names "Event", "Error"
You can add a section in your log4j.xml file with "com.mobios.common.Log4jUtil" and add the "Event" and "Error" handlers to it so that it logs to the corresponding files
<logger name="com.mobios.common.Log4jUtil" additivity="false">
<level value="debug" />
<appender-ref ref="EventAPPENDER" />
<appender-ref ref="ErrorAPPENDER" />
</logger>
While logging, you don't need to log the messages with getLogger() call again.
In your Log4jUtil.class, you can simply do,
logger.error("sdsffd")
In other classes, if you want to use the logger from Log4jUtil.class, you can do
Logger.getLogger(Log4JUtil.class) and this returns an already instantiated logger with the handlers attached
package com.mobios.common;
import org.apache.log4j.Logger;
public class AnotherClass {
public static final Logger logger= Logger.getLogger(Log4JUtil.class);
}
Related
I have the following log4j.xml configuration file, but don't see the log data stored in the file specified (in my documents folder: Documents/debug.log).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="true">
<appender name="applicationLogFile" class="org.apache.log4j.RollingFileAppender">
<param name="DatePattern" value="'.'yyyyMMdd'.log'" />
<param name="File" value="Documents/debug.log" />
<param name="threshold" value="TRACE" />
<param name="MaxNumberOfDays" value="90"/>
<param name="CompressBackups" value="TRUE"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c] %m%n" />
</layout>
</appender>
<logger name="APP_VERSION_LOGGER">
<appender-ref ref="applicationVersionFile" />
</logger>
<logger name="org.springframework">
<level value="WARN" />
</logger>
<root>
<priority value="DEBUG" />
<appender-ref ref="applicationLogFile" />
<appender-ref ref="bmcAlertFile" />
</root>
</log4j:configuration>
How an I get my log data sent to my Documents directory on my computer? I'm using log4j-core version 2.13.3. And yes, I've looke at the docs, but I still don't see where my log data is going.
Your line here says:
<param name="File" value="Documents/debug.log" />
It will make a folder with name Documents on your project root level and logs will be there.
I f you want to put them in to specific location on your machine then you need to provide full pathname.
ex.
<param name="File" value="/Users/myUser/Documents/debug.log" />
I have implemented a log4j XML file to create a new log file.in my local machine, when the tomcat starts the java web application, log file is being created without any issue.but i have hosted my java web application in aws instance configured with tomcat 7.but when i logged to aws instance and check tomcat log folder grid-services.log file cannot be located.can anyone tell why this happened?
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration debug="false">
<appender name="default.console" class="org.apache.log4j.ConsoleAppender">
<param name="target" value="System.out" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<appender name="default.file" class="org.apache.log4j.FileAppender">
<param name="file" value="${catalina.home}/logs/grid-services.log" />
<param name="append" value="false" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<logger name="com.customer"
additivity="false">
<level value="info" />
<appender-ref ref="default.file" />
</logger>
<root>
<priority value="info" />
<appender-ref ref="default.console" />
<appender-ref ref="default.file" />
</root>
</log4j:configuration>
Look in your ${TOMCAT_HOME}/bin/catalina.sh. Check if the
-Dcatalina.home=
variable is set on your aws instance.
In my Tomcat application, I want to use two loggers to log general event informations and errors to two different files.
Logger eventLogger = Logger.getLogger("event");
Logger errorLogger = Logger.getLogger("error");
Now I want to do the following: the logs of the eventLogger should be written to a "events.log" file, and the errorLogger logs should be written in a "errors.log" file. All logs from any other logger (e.g. Tomcat logs) should be written to catalina.out (or any other default file).
How can I achive this with JULI? Or do I need a third party lib?
This can't be achieved with Juli. You might use log4j.
Basically you need appenders. They will manage the logs to go to separate files. Example configuration in xml:
<?xml version="1.0"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.EnhancedPatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%t][%c{1}:%L] %m%n" />
</layout>
</appender>
<appender name="eventFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="#tomcat.home#/logs/event.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<appender name="errorFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="#tomcat.home#/logs/error.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<logger name="event" additivity="false">
<level value="INFO" />
<appender-ref ref="eventFILE" />
</logger>
<logger name="error" additivity="false">
<level value="INFO" />
<appender-ref ref="errorFILE" />
</logger>
<root>
<priority value="INFO" />
<appender-ref ref="FILE" />
</root>
</log4j:configuration>
you have to define an appender for each file in your log4j.xml and connect them to your loggers:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p [%c.%M:%L] - %m%n" />
</layout>
</appender>
<appender name="FILE-error" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${catalina.base}/logs/error.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="ImmediateFlush" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p [%c.%M:%L] - %m%n" />
</layout>
</appender>
<appender name="FILE-event" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${catalina.base}/logs/event.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="ImmediateFlush" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss};%m%n" />
</layout>
</appender>
<!-- Loggers -->
<logger name="error" additivity="false">
<level value="warn" />
<appender-ref ref="FILE-error" />
</logger>
<logger name="event" additivity="false">
<level value="info" />
<appender-ref ref="FILE-event" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="FILE-error" />
</root>
</log4j:configuration>
Tomcat documentation does not provide any way to define more than one file per application when using JULI. Actually it provides the steps to use log4j instead of it because JULI configuration can be too basic.
But again this configuration is at container level:
Note: The steps described in this section are needed when you want to
reconfigure Tomcat to use Apache log4j for its own logging. These
steps are not needed if you just want to use log4j in your own web
application. — In that case, just put log4j.jar and log4j.properties
into WEB-INF/lib and WEB-INF/classes of your web application.
So using a third party library is the logical choice and you can find how to configure it in this SO post
Tomcat JULI logging supports this only via the logging.properties located in the ${catalina.base}/config folder.
#Declares the handlers allowed for use.
handlers = 100catalina.org.apache.juli.FileHandler, 200catalina.org.apache.juli.FileHandler, 300catalina.org.apache.juli.FileHandler
#Install the 'all' hander on the root logger.
.handlers=300catalina.org.apache.juli.FileHandler
#Install the 'event' handler on the 'event' logger and don't report to root.
event.handlers=100catalina.org.apache.juli.FileHandler
event.useParentHandlers=false
#Install the 'error' handler on the 'error' logger and don't report to root.
error.handlers=200catalina.org.apache.juli.FileHandler
error.useParentHandlers=false
#Event handler settings.
100catalina.org.apache.juli.FileHandler.level = ALL
100catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
100catalina.org.apache.juli.FileHandler.prefix = event.
#Error handler settings.
200catalina.org.apache.juli.FileHandler.level = ALL
200catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
200catalina.org.apache.juli.FileHandler.prefix = error.
#Root handler settings.
300catalina.org.apache.juli.FileHandler.level = ALL
300catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
300catalina.org.apache.juli.FileHandler.prefix = all.
This config will not work if you try to install this file in the WEB-INF/classes/logging.properties. This is because the ClassLoaderLogManager doesn't allow you to install handlers on named loggers.
I'm trying to log to a file, if the logging is called from a specific package, and otherwise log to console. So I set the additivity to false for the two loggers which should be logging to file, so nothing would be logged on the console for these two packages. My log4j.xml is as follows:
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="TRACE"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} [%t] %-5p %c - %m%n"/>
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<!-- Set your own log file location -->
<param name="File" value="c:/temp/security-toolkit.log" />
<!-- Specify the number of backup -->
<param name="MaxBackupIndex" value="10"/>
<!-- Specify the file size -->
<param name="MaxFileSize" value="10MB"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p[%X{event} %X{user_name}:%X{user_sid}#%X{user_last_host_address} -> /%X{application_name}/%X{module_name}] %m%n"/>
</layout>
</appender>
<root>
<priority value="INFO"/>
<appender-ref ref="console"/>
</root>
<logger name="com.foo.security" additivity="false">
<level value="INFO" />
<appender-ref ref="file"/>
</logger>
<logger name="org.foo.esapi" additivity="false">
<level value="WARN" />
<appender-ref ref="file"/>
</logger>
However, logs from com.foo.security are still being logged to the console. Does anyone know what the issue could be here?
In a external file I would like to log the output of my com.foo.test class to a specific file but whats happening is its appending the console log as well as file log. Below is my xml configuration,
<appender name="FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/server.log"/>
<param name="Append" value="false"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
</layout>
</appender>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<appender name="MYTESTAPPENDER" class="org.jboss.logging.appender.RollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.home.dir}/log/mytest.log"/>
<param name="Append" value="false"/>
<param name="MaxFileSize" value="3MB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<logger name="testsplitlogger" additivity="false">
<level value="info"/>
<appender-ref ref="MYTESTAPPENDER"/>
</logger>
<root>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
<appender-ref ref="MYTESTAPPENDER"/>
</root>
And My Java code is,
private static final Logger logger = Logger.getLogger("testsplitlogger");
Update:
The problem is its logging all the console output and other appenders output to mytest.log file instead I want to append only the info messages from my testsplitlogger for specific class and other logs from console and file should be written to server.log.
Am I doing anything wrong?
Please see the <root> xml config. You are allowing the log to be directed on all three different files.
CONSOLE (system.out)
FILE(in this case server.log) and
MYTESTAPPENDER(mytest.log)
To your requirement you need to define like this.
<root>
<priority value="info"/>
<!--<appender-ref ref="CONSOLE"/>-->
<!--<appender-ref ref="FILE"/>-->
<appender-ref ref="MYTESTAPPENDER"/>
</root>
EDIT:
It seems you have updated the question since I last posted... So my following suggestion has already been taken care of by you.
The testsplitlogger must contain the appender reference as well.
<logger name="testsplitlogger" additivity="false">
<level value="info" />
<appender-ref ref="MYTESTAPPENDER" />
</logger>
Here is a good link you can refer to http://wiki.apache.org/logging-log4j/Log4jXmlFormat
You need to create a Category for that:
<category name="my.company.package" additivity="false">
<priority value="info" />
<appender-ref ref="MYTESTAPPENDER" />
</category>