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.
Related
I want to use an log4j-Appender multiple times, but with different filenames:
Appender:
<RollingFile name="MODULE" fileName="log/module-${ModuleName}.log" append="true" filePattern="log/module-${ModuleName}-%d{yyyy-MM-dd}_%i.log" >
<PatternLayout pattern="%d{yyyy/MM/dd-HH:mm:ss} %-5p %C:%M:%L ~ %m%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="100MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
Loggers:
<Logger name="MOD1" level="debug">
<Properties>
<Property name="ModuleName">MOD1</Property>
</Properties>
<appender-ref ref="MODULE"/>
</Logger>
<Logger name="MOD2" level="error">
<Properties>
<Property name="ModuleName">MOD2</Property>
</Properties>
<appender-ref ref="MODULE"/>
</Logger>
But this don't work. How can I pass a Variable from the Logger to the Appender?
Finally I want 2 Files
/log/module-MOD1.log <- Debug messages from MOD1
/log/module-MOD2.log <- Error messages from MOD2
Thanks for any help
Just add another Appender and connect Logger and Appender each pair.
For example :
...
<RollingFile name="MODULE1" fileName="./log/module1-${ModuleName}.log" append="true" filePattern="./log/module1-${ModuleName}-%d{yyyy-MM-dd}_%i.log" >
<PatternLayout pattern="%d{yyyy/MM/dd-HH:mm:ss} %-5p %C:%M:%L ~ %m%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="100MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="MODULE2" fileName="./log/module2-${ModuleName}.log" append="true" filePattern="./log/module2-${ModuleName}-%d{yyyy-MM-dd}_%i.log" >
<PatternLayout pattern="%d{yyyy/MM/dd-HH:mm:ss} %-5p %C:%M:%L ~ %m%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="100MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
...
...
<Logger name="com.foo.bar.ClassName1" level="debug">
<Properties>
<Property name="ModuleName">MOD1</Property>
</Properties>
<appender-ref ref="MODULE1"/>
</Logger>
<Logger name="com.foo.bar.ClassName2" level="error">
<Properties>
<Property name="ModuleName">MOD2</Property>
</Properties>
<appender-ref ref="MODULE2"/>
</Logger>
...
There was a same ask so I replied there too. so, please visit here.
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)-->
following XML configuration is my log4j config, I see the log messages in console but the log file is empty, whats wrong in it ?
I am using log4j-api-2.1
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<RollingFile name="file-log" fileName="${log-path}/logtile.log/"
filePattern="${log-path}/logtile-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>%d{HH:mm:ss.SSS} [%t] %-5level - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="500" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="mylogger" level="info" additivity="false">
<appender-ref ref="file-log" level="info" />
<appender-ref ref="Console" level="info" />
</Logger>
<Root level="info">
<appender-ref ref="Console" />
</Root>
</Loggers>
How does your code obtain Logger instances? Assuming your code looks something like this:
Logger log = LogManager.getLogger(com.mycompany.MyClass.class);
log.info("Where is my message?");
Note that the fully qualified name of your logger is "com.mycompany.MyClass".
This logger will send messages to the root logger, and all named loggers whose name matches "com.mycompany.MyClass". The following configuration names would match this fully qualified class:
<Logger name="com.mycompany.MyClass" ...>
<Logger name="com.mycompany" ...>
<Logger name="com" ...>
However, the named logger in your configuration only matches "mylogger", so this logger will not receive the "Where is my message?" log event. The root logger will get the event and send it to its appender, the console.
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.