I'm wondering if the following is possible:
Based on the standard log4j2.xml file, I would like to insert the "Completed" string to the previous file, when switching to a new one.
Something like: "logs/app-09-18-2017_Completed.log"
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log"
<PatternLayout>
<Pattern>%m%n</Pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="SYSTEM_OUT"/>
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
Chaing the filePattern to
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app_completed-%d{MM-dd-yyyy}.log">
shall help you. Since it treats the pattern as a string where the the DateFormat %d or the integer count %i is replaced.
Related
Below is my log4j2.xml'
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="LogToConsole" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="LogToFile" fileName="logs/myapp_-${date:yyyyMMddHHmmss}.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="LogToConsole"/>
<AppenderRef ref="LogToFile"/>
</Root>
</Loggers>
</Configuration>
It generates log files based on timestamp in logs/ directory.
I want to print the logfile name ( value of LogToFile variable ) in my java code and give to user so that he can come to know what is the exact location and name of the log file of his run.
Any idea if this is possible?
Kind Regards,
Abhi
I am trying to change log levels programmatically for a given keyword. For example If I have the log level set to OFF but want to see logs that contain "database" keyword, how can I do that? Is there a way to to that via java?
#fatCop's answer is mostly correct. You can start with log4j2.xml configured as
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="AppName" packages="">
<RegexFilter regex="*database*" onMatch="DENY" onMismatch="DENY"/>
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
Because this is using a global filter it will cause nothing to be logged. When you are ready to enable logging do
final LoggerContext loggerContext = LoggerContext.getContext(false);
final Configuration config = loggerContext.getConfiguration();
Filter filter = RegexFilter.createFilter("database", null, false, Result.ACCEPT, RESULT.DENY);
config.setFilter(filter);
When you want it disabled then replace the filter with a new one that is set back to DENY on a match.
Note that the logging level on the root logger is irrelevant since the filter is only accepting or denying events.
As per Log4j Filter Manual, you can use RegexFilter. Snippet referring to manual, the configuration for you can be like:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="AppName" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<RegexFilter regex="*database*" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
private static final Logger log = LogManager.getLogger(ABC.class);
private static final Logger resultLog = LogManager.getLogger("ResultLog");
How can we define a appender that would only logs specific to "ResultLog" logger?
My current Log4j2.xml file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="ABC" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/ABC.log"
filePattern="logs/$${date:yyyy-MM}/ABC-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d [%t] %p %c{1.} %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
<File name="resultFile" fileName="logs/result.log">
<PatternLayout pattern="%m%n"/>
</File>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %p %c{1.} %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="RollingFile"/>
<AppenderRef ref="Console"/>
<AppenderRef ref="resultFile"/>
</Root>
</Loggers>
</Configuration>
thanks
Add
<Logger name="ResultLog">
<AppenderRef ...>
</Logger>
to the Loggers tag.
You can refer to this manual: https://logging.apache.org/log4j/2.x/manual/configuration.html#XML
I'm looking for a log4j2.properties snippet that I can drop in to src/test/resources just to make the No logj2 configuration file found warning disappear. If it contains other useful sample like configuration examples that's great as well.
Basically I'm looking for the .properties version of this:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss} %c{1.} [%t] %-5level} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
I haven't tried it but this should do the trick.
status = error
name = PropertiesConfig
property.filename = target/logs/test.log
appender.file.type = File
appender.file.name = LogFile
appender.file.fileName = ${filename}
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d %p %C{1.} [%t] %m%n
rootLogger.level = error
rootLogger.appenderRef.stdout.ref = LogFile
Here is a log4j2.xml that I actually use in some of my unit tests.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ERROR">
<properties>
<property name="LOG_DIR">target/logs</property>
</properties>
<MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{ABSOLUTE} %-5level # %class.%method %m%n" />
</Console>
<RollingFile name="log4j" fileName="${LOG_DIR}/log4j.txt" filePattern="${LOG_DIR}/archive/log4j.txt.%d{yyyyMMdd_HH}-%i">
<PatternLayout>
<MarkerPatternSelector defaultPattern="%d [%t] %-5p %X{loginId, userId, ipAddress, corpAcctNumber} %C{1.}.%M:%L - %m%n">
<PatternMatch key="FLOW" pattern="%d [%t] %-5p %X{loginId, userId, ipAddress, corpAcctNumber} -------- %C{1.}.%M:%L %msg --------%n"/>
</MarkerPatternSelector>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="30 MB"/>
</Policies>
<DefaultRolloverStrategy min="1" max="20"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="log4j" />
</Root>
</Loggers>
</Configuration>
I am new in Log4j2 and want to use the RollingFileAppender. Further as rollover I want to use the TimeBaseTriggeringPolicy:
The TimeBasedTriggeringPolicy causes a rollover once the date/time pattern no longer applies to the active file.
On the site above there is an example for such an TimeBasedTriggeringPolicy:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
Where is in the configuration above the "date/time pattern" defined, that determines if the active file applies to it or not?
Thanks for your help!
That is the filePattern. In your config:
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
The most granular time unit in the above is dd (days) so it will rollover daily at midnight.