The log error messages are getting printed to all the log files instead of the error file. Not sure what i have missed in the configuration. Can anyone check and let me know
I tried all permutations and combinations but nothing seems to be working.
I think i am missing some configuration but not sure what it is.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%X{id}] ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<RollingFile name="ROOTLOGS" fileName="${sys:LOG_DIR}bot.log"
filePattern="${sys:LOG_DIR}bot-%d{yyyy-MM-dd}-%i.log.gz" >
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}- [%-5level] : %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
<ThresholdFilter level="DEBUG"/>
</RollingFile>
<RollingFile name="ERRORLOGS" fileName="${sys:LOG_DIR}boterror.log"
filePattern="${sys:LOG_DIR}boterror-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
<ThresholdFilter level="ERROR"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.org.bot" level="DEBUG" additivity="false">
<appender-ref ref="ERRORLOGS" />
<appender-ref ref="ROOTLOGS" />
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="ROOTLOGS" />
<AppenderRef ref="ERRORLOGS" />
</Root>
</Loggers>
</Configuration>
Expectation is that error messages should only get printed to boterror.log file and normal application messages should get printed to bot.log.
DENY error messages from ROOTLOGS.
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<RollingFile name="ROOTLOGS" fileName="${sys:LOG_DIR}bot.log"
filePattern="${sys:LOG_DIR}bot-%d{yyyy-MM-dd}-%i.log.gz" >
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}- [%-5level] : %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT"/>
</RollingFile>
<RollingFile name="ERRORLOGS" fileName="${sys:LOG_DIR}boterror.log"
filePattern="${sys:LOG_DIR}boterror-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
<ThresholdFilter level="ERROR"/>
</RollingFile>
</Appenders>
This is what finally did the trick. Whatever sazzad has suggested + i need to add threshold filter to ERRORLOG without which normal logs were also getting added to it.
This is the final configuration which is working as per my expectations
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%X{id}] ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<RollingFile name="ROOTLOGS" fileName="${sys:LOG_DIR}bot.log"
filePattern="${sys:LOG_DIR}bot-%d{yyyy-MM-dd}-%i.log.gz" >
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}- [%-5level] : %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT"/>
</RollingFile>
<RollingFile name="ERRORLOGS" fileName="${sys:LOG_DIR}boterror.log"
filePattern="${sys:LOG_DIR}boterror-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
<ThresholdFilter level="ERROR"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.org.bot" level="DEBUG" additivity="false">
<appender-ref ref="ERRORLOGS" />
<appender-ref ref="ROOTLOGS" />
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="ROOTLOGS" />
<AppenderRef ref="ERRORLOGS" />
</Root>
</Loggers>
</Configuration>
Related
My programs stops log in files just by replacing:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
with
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
I'm using : OracleJdk8, log4j-core-2.17.1 and slf4j-sample-2.0.0-alpha6
Please find bellow my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">C:/path/to/logfile</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{dd/MM/yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
<RollingFile name="functional-log" fileName="${log-path}/functional.log" filePattern="${log-path}/functional-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{dd/MM/yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY" />
</RollingFile>
<RollingFile name="technical-1-log" fileName="${log-path}/technical.log" filePattern="${log-path}/technical-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{dd/MM/yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<LevelRangeFilter minLevel="DEBUG" maxLevel="TRACE" onMatch="ACCEPT" onMismatch="DENY" />
</RollingFile>
<RollingFile name="technical-2-log" fileName="${log-path}/technical.log" filePattern="${log-path}/technical-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{dd/MM/yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<LevelRangeFilter minLevel="WARN" maxLevel="WARN" onMatch="ACCEPT" onMismatch="DENY" />
</RollingFile>
<RollingFile name="error-log" fileName="${log-path}/error.log" filePattern="${log-path}/error-%d{yyyy-MM-dd}.err">
<PatternLayout>
<pattern>[%-5level] %d{dd/MM/yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="x.y.z" level="ALL" additivity="false">
<appender-ref ref="console-log" level="ALL" />
<appender-ref ref="functional-log" level="INFO" />
<appender-ref ref="technical-1-log" level="TRACE" />
<appender-ref ref="technical-2-log" level="WARN" />
<appender-ref ref="error-log" level="ERROR" />
</Logger>
<Root level="TRACE" additivity="false">
<AppenderRef ref="console-log" />
</Root>
</Loggers>
</Configuration>
By this configuration, I'd like to log :
"ERROR" and "FATAL" in error.err file.
"TRACE", "DEBUG" and "WARN" in technical.log file.
"INFO" in functional.log file.
Could you please indicate me the right way to use slf4j import to still logging in RollingFile?
Knowing that org.slf4j.Logger.fatal() method doesn't exist :(
Thank you so much for you help!
I am not interested in printing logs to console, only to file.
my current Log4j2.xml configuration doesn't support this unless I am configuring 'level=" OFF"' but then it doesn't write to both, file and console.
how do I configure the logger to write only to file?
already tried to comment out the tag and to comment out the "appenderRef" tag inside the "Logger" tag
the log4j2.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
<Appenders>
<RollingFile name="vertx_file" append="true"
fileName="/var/log/vertx.log"
filePattern="/var/log/vertx/$${date:yyyy-MM}/vertx-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="%d{ISO8601} %-5p %c:%L - %m%n" />
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="5MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
<Console name="STDOUT" target="SYSTEM_OUT">
<!-- <LogStashJSONLayout/> -->
<PatternLayout pattern="%d{ISO8601} %-5p %c:%L - %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.service.vertex" level="OFF" >
<!-- <AppenderRef ref="vertx_rollingFile" /> -->
<!-- <AppenderRef ref="vertx_socket" /> -->
<!-- <AppenderRef ref="STDOUT" /> -->
<AppenderRef ref="vertx_file" />
</Logger>
<Logger name="org.mongodb.driver" level="OFF">
<AppenderRef ref="vertx_rollingFile" />
<AppenderRef ref="vertx_socket" />
<AppenderRef ref="STDOUT" />
<AppenderRef ref="vertx_file" />
</Logger>
<Root level="INFO">
<AppenderRef ref="vertx_socket" />
<!--<AppenderRef ref="STDOUT" />-->
<AppenderRef ref="vertx_file" />
</Root>
</Loggers>
</Configuration>
the log:
Apr 03, 2019 2:02:35 PM com.service.vertex.dao.UsersCollection
INFO: {"response_code":551,"description":"INVALIDATE SESSION ERROR: null","log_origin":"system","id":"ERROR (57) 14:02:35:050","time":"2019-04-03 14:02:35:050","host_name":"pc","server_port":"444","level":"ERROR","verticle":"admin","remote_address":"172.15.333.21"}
still printing to console
Comment the below in the config and try
<Console name="STDOUT" target="SYSTEM_OUT">
<!-- <LogStashJSONLayout/> -->
<PatternLayout pattern="%d{ISO8601} %-5p %c:%L - %m%n" />
</Console>
and the appenders
<AppenderRef ref="STDOUT" />
We have a situation where the server is in Germany and we're based in the UK. We want the log4net logs to show a timestamp that is GMT, but I can't work out how or if it's possible to add an hour to the time being written to the log file.
This is our current config:
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<file value="logs\\" />
<datePattern value="dd.MM.yyyy'.log'" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="50" />
<maximumFileSize value="5MB" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-dd-MM HH:mm:ss.fff} %-5level - %message%newline" />
</layout>
</appender>
I found some references online to an "hourOffset", but this doesn't seem to work and never appears in the log4net documentation.
Any advise here would be welcome.
Use utcdate instead of d (or date). See also the documentation.
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%utcdate{yyyy-dd-MM HH:mm:ss.fff} %-5level - %message%newline" />
</layout>
I need to print each level logs in a seperate file. For example, Debug msg alone should print in debug file and not other level logs should print in debug file..
I am new to logs , so can you correct the code.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorinterval="30" status="info" strict="true">
<Properties>
<Property name="debug">D://logs/debug.log</Property>
<Property name="info">D://logs/info.log</Property>
<Property name="warn">D://logs/warn.log</Property>
<Property name="error">D://logs/error.log</Property>
<Property name="fatal">D://logs/fatal.log</Property>
</Properties>
<Appenders>
<Appender type="File" name="Debug_file" fileName="${debug}">
<Layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n" />
</Appender>
<Appender type="File" name="Info_file" fileName="${info}">
<Layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n" />
</Appender>
<Appender type="File" name="Warn_file" fileName="${warn}">
<Layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n" />
</Appender>
<Appender type="File" name="Error_file" fileName="${error}">
<Layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n" />
</Appender>
<Appender type="File" name="Fatal_file" fileName="${fatal}">
<Layout type="PatternLayout" pattern="%d %p %C{1.} [%t] %m%n" />
</Appender>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>
There are at least 2 different ways you could achieve what you want. One way is to use the LevelRangeFilter (see log4j2 javadoc) as shown in the following example configuration:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="LOG_PATTERN">%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Property>
</Properties>
<Appenders>
<File name="TraceFile" fileName="logs/Trace.log" immediateFlush="true"
append="true">
<PatternLayout pattern="${LOG_PATTERN}" />
<LevelRangeFilter minLevel="TRACE" maxLevel="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>
</File>
<File name="DebugFile" fileName="logs/Debug.log" immediateFlush="true"
append="true">
<PatternLayout pattern="${LOG_PATTERN}" />
<LevelRangeFilter minLevel="DEBUG" maxLevel="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
</File>
<File name="InfoFile" fileName="logs/Info.log" immediateFlush="true"
append="true">
<PatternLayout pattern="${LOG_PATTERN}" />
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</File>
<File name="WarnFile" fileName="logs/Warn.log" immediateFlush="true"
append="true">
<PatternLayout pattern="${LOG_PATTERN}" />
<LevelRangeFilter minLevel="WARN" maxLevel="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
</File>
<File name="ErrorFile" fileName="logs/Error.log" immediateFlush="true"
append="true">
<PatternLayout pattern="${LOG_PATTERN}" />
<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
</File>
<File name="FatalFile" fileName="logs/Fatal.log" immediateFlush="true"
append="true">
<PatternLayout pattern="${LOG_PATTERN}" />
<LevelRangeFilter minLevel="FATAL" maxLevel="FATAL" onMatch="ACCEPT" onMismatch="DENY"/>
</File>
</Appenders>
<Loggers>
<Root level="TRACE">
<AppenderRef ref="TraceFile" />
<AppenderRef ref="DebugFile" />
<AppenderRef ref="InfoFile" />
<AppenderRef ref="WarnFile" />
<AppenderRef ref="ErrorFile" />
<AppenderRef ref="FatalFile" />
</Root>
</Loggers>
</Configuration>
The above configuration ensures that each appender accepts log events of exactly one level only.
Another option is to use the RoutingAppender (see log4j2 manual) as demonstrated in the following configuration:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="LOG_PATTERN">%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Property>
</Properties>
<Appenders>
<File name="TraceFile" fileName="logs/Trace.log" immediateFlush="true"
append="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</File>
<File name="DebugFile" fileName="logs/Debug.log" immediateFlush="true"
append="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</File>
<File name="InfoFile" fileName="logs/Info.log" immediateFlush="true"
append="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</File>
<File name="WarnFile" fileName="logs/Warn.log" immediateFlush="true"
append="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</File>
<File name="ErrorFile" fileName="logs/Error.log" immediateFlush="true"
append="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</File>
<File name="FatalFile" fileName="logs/Fatal.log" immediateFlush="true"
append="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</File>
<Routing name="Routing">
<Routes>
<Script name="RoutingInit" language="JavaScript"><![CDATA[
logEvent.getLevel();]]>
</Script>
<Route ref="TraceFile" key="TRACE" />
<Route ref="DebugFile" key="DEBUG" />
<Route ref="InfoFile" key="INFO" />
<Route ref="WarnFile" key="WARN" />
<Route ref="ErrorFile" key="ERROR" />
<Route ref="FatalFile" key="FATAL" />
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="TRACE">
<AppenderRef ref="Routing" />
</Root>
</Loggers>
</Configuration>
In this configuration the appenders are indifferent toward the level of the log event. The RoutingAppender is the decision maker with regard to which events go to which appender. I tend to prefer using RoutingAppender because it can be made more compact by taking advantage of the log4j2 plugin system. If you add your own lookup which returns the name of the level associated with the log event you can use that lookup to dynamically create appenders on the fly. The code for the lookup and the revised configuration are shown below:
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.lookup.StrLookup;
#Plugin(name = "level", category = "Lookup")
public class LevelLookup implements StrLookup{
/**
* Lookup the value for the key.
* #param key the key to be looked up, may be null
* #return The value for the key.
*/
public String lookup(String key) {
return null;
}
/**
* Lookup the value for the key using the data in the LogEvent.
* #param event The current LogEvent.
* #param key the key to be looked up, may be null
* #return The value associated with the key.
*/
public String lookup(LogEvent event, String key) {
return event.getLevel().name();
}
}
Here is the revised configuration that takes advantage of the new lookup:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="LOG_PATTERN">%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Property>
</Properties>
<Appenders>
<Routing name="Routing">
<Routes pattern="$${level:}">
<Route>
<File name="${level:}File" fileName="logs/${level:}.log" immediateFlush="true"
append="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</File>
</Route>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="TRACE">
<AppenderRef ref="Routing" />
</Root>
</Loggers>
</Configuration>
As with the other configurations this will send log messages to a file based on the log level of the log event. For example INFO level events will be logged to logs/INFO.log
I hope this helps you.
Here is my Nlog.Config code.
<nlog>
<variable name="logFilePath" value="C:\NLog\IDG-${shortdate}.log" />
<targets>
<target name="logfile"
xsi:type="File"
fileName="${logFilePath}"
layout="${longdate} LEVEL=${level:upperCase=true}: ${message}"
keepFileOpen="true" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
<logger name="*" minlevel="Info" writeTo="logfile" />
<logger name="*" minlevel="Warn" writeTo="file"/>
</rules>
</nlog>
and am defining this in my class
private static Logger logger = LogManager.GetCurrentClassLogger();
and then I'm logging the error as:
catch (Exception ex)
{
logger.Error(ex.Message,"test");
}
Can anyone please suggest me if there is any better way to do this, I don't see the file logging in the destined folder.
It seems that you may not be referencing the config section correctly. Try changing your main config file to something like this and remove the Nlog.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable name="logFilePath" value="C:\temp\output.log" />
<targets>
<target name="logfile"
xsi:type="File"
fileName="${logFilePath}"
layout="${longdate} LEVEL=${level:upperCase=true}: ${message}"
keepFileOpen="true" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
<logger name="*" minlevel="Info" writeTo="logfile" />
<logger name="*" minlevel="Warn" writeTo="file"/>
</rules>
</nlog>
</configuration>