I have made a project in which I am creating logs with log4j2.xml. My configuration file works perfectly when I am running it in the eclipse of my windows system. Here is the configuration file which I have used to create the log files.
<?xml version="1.0" encoding="UTF-8" standalone="no"?><Configuration status="WARN">
<Properties>
<Property name="log-path">/home/apps/ora_fin/IGL75D/Disco/splunk/log</Property>
</Properties>
<Appenders>
<RollingFile fileName="${log-path}/SplunkOADC.log" filePattern="${log-path}/SplunkOADC-%d{yyyy-MM-dd}.log" name="info-log">
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<RollingFile fileName="${log-path}/SplunkOADC-trace.log" filePattern="${log-path}/SplunkOADC-trace-%d{yyyy-MM-dd}.log" name="trace-log">
<Filters>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="fatal" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
<ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="FATAL" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger level="all" name="com.aviva.uk.gi.NUProcessOSBReportDataParserPkg">
<AppenderRef ref="info-log"/>
<AppenderRef ref="trace-log"/>
</Logger>
<Root>
</Root>
</Loggers>
</Configuration>
This configuration file I have put under the src folder in my project and is picked up without any issues. However I need to deploy the application in the Unix environment, where should I put the configuration file in unix environment so that it is picked up by the java class to log ??
Thanks!
How about using a jvm argument always
-Dlog4j.configuration={path to file}
This should work irrespective of the environment you deploy in.
The simplest thing to do is to just add the config file to the classpath of your application.
For a web app that means putting it in WEB-INF/classes/.
Alternatively you can use the system property suggested in the other answer.
Related
I have a storm bolt that is responsible for extracting the contents of an email. I would like to log some details about the email in a log file of my choosing instead of in worker.log. The bolt does log to worker.log for errors etc. and I want it to continue to do so.
I've updated the cluster.xml to include the new logger
<configuration monitorInterval="60" shutdownHook="disable">
<properties>
<property name="pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} %c{1.} %t [%p] %msg%n</property>
</properties>
<appenders>
<RollingFile name="A1" immediateFlush="false"
fileName="${sys:storm.log.dir}/${sys:logfile.name}"
filePattern="${sys:storm.log.dir}/${sys:logfile.name}.%i.gz">
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="9"/>
</RollingFile>
<RollingFile name="TEST-SIZE" immediateFlush="false"
fileName="${sys:storm.log.dir}/test_debug.log"
filePattern="${sys:storm.log.dir}/test_debug.log.%i.gz">
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="9"/>
</RollingFile>
<Syslog name="syslog" format="RFC5424" charset="UTF-8" host="localhost" port="514"
protocol="UDP" appName="[${sys:daemon.name}]" mdcId="mdc" includeMDC="true"
facility="LOCAL5" enterpriseNumber="18060" newLine="true" exceptionPattern="%rEx{full}"
messageId="[${sys:user.name}:S0]" id="storm" immediateFlush="true" immediateFail="true"/>
</appenders>
<loggers>
<Logger name="TestSizeLogger" level="info" additivity="false">
<AppenderRef ref="TEST-SIZE"/>
<AppenderRef ref="syslog"/>
</Logger>
<root level="info"> <!-- We log everything -->
<appender-ref ref="A1"/>
<appender-ref ref="syslog"/>
</root>
</logger>
In my Java code I then create two logger instances but they both log to worker.log
static Logger logger = LoggerFactory.getLogger(ExtractorBolt.class);
static Logger testLogger = LoggerFactory.getLogger("TestSizeLogger");
I would appreciate any advice in regards to how I can get my testLogger to log to my new log file.
cluster.xml is for configuring the logging for Nimbus, the supervisor and other daemons. You should edit the log4j2/worker.xml file to configure worker logging.
According to this log4j2 configuration I am trying to set the "log4j2.component.properties" file in my NetBeans project. I have already configured my log4j2 with ASYNC_LOGGERS and one thing I particularly need is the "log4j.Clock" property. Because I get the data at a very high speed from the Servers and we expect that the logging timestamp needs to be perfect.
How can I use the "log4j2.component.properties" file in my project. Currently I created the "log4j2.component.properties" file in my default package of the project and set the values as below:
My log4j2.component.properties file
Log4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
AsyncLogger.RingBufferSize=512*1024
-DAsyncLoggerConfig.RingBufferSize=512*1024
log4j.Clock=SystemClock
My log4j2.xml file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<RollingRandomAccessFile name="Messages-log" fileName="Log4J/Messages-${date:yyyy-MM-dd}.log"
immediateflush="true" filePattern="Log4J/Messages-%d{MM-dd-yyyy-HH}-%i.log.gz">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="500 MB"/>
</Policies>
<DefaultRolloverStrategy max="50"/>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="Error-log" fileName="Log4J/Errors-${date:yyyy-MM-dd}.log"
immediateflush="false" filePattern="Log4J/Errors-%d{MM-dd-yyyy-HH}-%i.log.gz">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %p %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="500 MB"/>
</Policies>
<DefaultRolloverStrategy max="50"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Logger name="Messages-log" level="info" additivity="false">
<appender-ref ref="Messages-log" level="info"/>
</Logger>
<Logger name="Error-log" level="info" additivity="false">
<appender-ref ref="Error-log" level="info"/>
</Logger>
<root level="info">
<appender-ref ref="Messages-log"/>
<appender-ref ref="Error-log"/>
</root>
</Loggers>
</Configuration>
Is this correct way of using it? I am highly confused in how to use it. I want the timestamp to be perfect and so I want to use the "log4j2.clock" parameter set to "org.apache.logging.log4j.core.util.Clock". All I want is asynchronous logging with the exact time when the record has reached. But I am getting the following exception:
ERROR StatusLogger Could not create org.apache.logging.log4j.core.util.Clock: java.lang.InstantiationException: org.apache.logging.log4j.core.util.Clock, using default SystemClock for timestamps.
Please help me.
Thanks in advance
Im new here, Please help me to understand my new project's log4j2 configuration.
My question is:
How to get all Log outputs?
From where should I search log files?
Also how to save tomcat console outputs in the txt file?
I really appreciate your help and support, Today I want to learn something new from you guys! Thanks!
This is the log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="error" monitorInterval="1800">
<Properties>
<Property name="LOG_HOME">\Workspaces\logs\paymentweb</Property>
<Property name="LOG_DEBUG">${LOG_HOME}\app\debug.log</Property>
<Property name="LOG_INFO">${LOG_HOME}\app\info.log</Property>
<Property name="LOG_ERROR">${LOG_HOME}\app\error.log</Property>
</Properties>
<appenders>
<Console name="Console" target="SYSTEM_OUT">
(onMismatch)-->
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"/>
</Console>
<RollingRandomAccessFile name="app_debug" fileName="${LOG_DEBUG}" append="false" filePattern="${LOG_HOME}\$${date:yyyy-MM}\debug-%d{MM-dd-yyyy}-%i.log.gz">
<Filters>
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
</Filters>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="50 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</RollingRandomAccessFile>
<CustomRollingRandomAccessFile name="app_info" fileName="${LOG_INFO}" append="false" filePattern="${LOG_HOME}\$${date:yyyy-MM}\info-%d{MM-dd-yyyy}-%i.log.gz">
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="50 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</CustomRollingRandomAccessFile>
<CustomRollingRandomAccessFile name="app_error" fileName="${LOG_ERROR}" append="false" filePattern="${LOG_HOME}\$${date:yyyy-MM}\error-%d{MM-dd-yyyy}-%i.log.gz">
<Filters>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="50 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</CustomRollingRandomAccessFile>
</appenders>
<loggers>
<root level="trace" additivity="false">
<appender-ref ref="Console"/>
<appender-ref ref="app_debug"/>
<appender-ref ref="app_info"/>
<appender-ref ref="app_error"/>
</root>
</loggers>
</configuration>
all your logs are in \Workspaces\logs\paymentweb\app*.log
You can find out details about how your appenders are configured by enabling internal Log4j2 "status" logging.
At the top of your configuration file, change it to TRACE <configuration status="trace" monitorInterval="1800">. I believe this will show the full path of the file appenders on the console. (No guarantees for the custom appender CustomRollingRandomAccessFile.)
Also, you have a non-XML compliant snippet inside the <Console> appender configuration. This looks bad and should be removed:
(onMismatch)-->
I've been searching the web for days now but somehow nothing seems to help.
I try to migrate from log4j 1.x to log4j 2 but something is not right here and I hope one of you can help.
I have a JSF application running on Websphere 8 and this is is my configuration so far:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Filter type="ThresholdFilter" level="trace"/>
<Appenders>
<RollingFile name="DEBUG" filePattern="C:/Files/varm_debug.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_debug.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<RollingFile name="INFO" filePattern="C:/Files/varm_info.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_info.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<RollingFile name="WARN" filePattern="C:/Files/varm_warn.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_warn.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<RollingFile name="ERROR" filePattern="C:/Files/varm_error.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_error.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<File name="comparisonLogAppender" fileName="C:/Files/VARM-output.log" append="true">
<PatternLayout pattern="%d{ISO8601}; %m%n"/>
</File>
</Appenders>
<Loggers>
<!-- Logger to compare results -->
<!-- Logger to log infos/errors throughout comparison -->
<Logger name="comparisonLogger" level="trace" additivity="true" immediateFlush="true">
<AppenderRef ref="comparisonLogAppender"/>
</Logger>
<Logger name="de.saltsolutions.varm" level="info" >
<AppenderRef ref="DEBUG" level="debug"/>
<AppenderRef ref="INFO" level="info"/>
<AppenderRef ref="WARN" level="warn"/>
<AppenderRef ref="ERROR" level="error"/>
</Logger>
<Logger name="org.hibernate" level="warn">
<AppenderRef ref="DEBUG" level="debug"/>
<AppenderRef ref="INFO" level="info"/>
<AppenderRef ref="WARN" level="warn"/>
<AppenderRef ref="ERROR" level="error"/>
</Logger>
<Root level="debug">
<AppenderRef ref="DEBUG" level="debug"/>
</Root>
<Root level="info">
<AppenderRef ref="INFO" level="info"/>
</Root>
<Root level="warn">
<AppenderRef ref="WARN" level="warn"/>
</Root>
<Root level="error">
<AppenderRef ref="ERROR" level="error"/>
</Root>
</Loggers>
</Configuration>
All the files are generated okay but they stay empty.
My main problem is the comparisonLogger. If I use it (LogManager.getLogger("comparisonLogger") in my main class, it writes the log file perfectly.
But when I try using the exact same logger in my ManagedBean (ViewScoped) nothing gets logged.
Does anybody know why it doesn't work in my ManagedBean? Does this have anything to do with the Websphere?
I hope anybody can help because I have absolutely no idea what I could possibly do to make this work.
Thanks so much in advance
I finally figured out my problems.
I use Log4j2 in a web application which means that I needed to add some stuff in my web.xml (further information here: http://logging.apache.org/log4j/2.x/manual/webapp.html)
You can only configure ONE root logger
Once I adjusted that, everything worked just as expected.
My Log4j2 Jar-Files are now in my client and my ear and my XML config is in my client but it also works if you split the config files (one part in my web project the other for the client on C:\temp or some other directory)
You may need to put the log4j2 jar files and log4j2.xml config in a place so that they are always in the classpath.
I am currently working on an application which will generate 2 different log files for different purposes. Since I am new to log4j2, I am unable to achieve it. Here is my configuration file (log4j2.xml) :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">C:/Users/460681/Desktop/SourceFiles</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout
pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
<RollingFile name="info-log" fileName="${log-path}/SplunkOADC.log"
filePattern="${log-path}/SplunkOADC-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="trace" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/SplunkOADC-error.log"
filePattern="${log-path}/SplunkOADC-error-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="trace" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
</RollingFile>
<RollingFile name="trace-log" fileName="${log-path}/SplunkOADC-trace.log"
filePattern="${log-path}/SplunkOADC-trace-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="info-log" level="info"/>
<AppenderRef ref="trace-log" level="trace"/>
</Root>
</Loggers>
</Configuration>
I have tried filters but I'm not sure if it is the correct way. Here is my java method which is trying to log using log4j2
logger.entry("Enter The app");
String report_index_data =
"select REPORT_MODE, INDEX_ID from TABLE_NAME";
ResultSet rs = db.selectQuery(report_index_data, conn);
while(rs.next()){
logger.info("report_index_data =" +rs.getString("report_index_data"));
}
logger.exit();
Thanks!
Add a new logger to configuration.
<root level="debug">
<appender-ref ref="info-log" level="info"/>
</root>
<logger name="logger2">
<AppenderRef ref="trace-log" level="trace"/>
</logger>
First, note that if your root logger level is debug, then it will not send trace-level log events to any of its appenders; it will only debug level or higher log events to its appenders.
Second, your code uses logger.entry() and logger.exit(). For these methods to be useful you need to use a pattern layout with location patterns like %location or %method in the pattern. (E.g. [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1}.%M - %msg%n). This will display the name of the method you entered/exited. Be aware that calculating location information has some performance impact.