grails / log4j / conversion pattern / %throwable - grails

I use log4j 1.2.16 regarding my "dependency-report".
my Conversion Pattern is '%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c: %m%n %throwable{short}'
but %throwable is not recognized, instead the loglines contain
'...hrowable{short}...'
Any idea ?

I assume this is in an appender? Are you using EnhancedPatternLayout (I believe you need that for %throwable{n})
console name: 'stdout', layout: new EnhancedPatternLayout(conversionPattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c: %m%n %throwable{short}")

Related

Log4J2 Nested Property Substitution

I am attempting to use Log4J2 property substitution to simplify my deployed log4j2.xml file. I want to share it between three deployed WAR files, so I've defined a system property detailing the external location of that file.
Now I've run into a different issue. I've added the following to my web.xml:
<context-param>
<param-name>loggingPrefix</param-name>
<param-value>app1</param-value>
</context-param>
In the log4j2.xml, I've created the following:
<Console name="CONSOLE_LOG" target="SYSTEM_OUT">
<PatternLayout pattern="$upper{${web:loggingPrefix}}> %d{dd MMM HH:mm:ss} %5p [%t] (%F:%L) - %m%n" />
</Console>
What happens is that the loggingPrefix is properly output, but the log entry only prints out as $upper{app1}. I have also attempted to specify the $upper as $$upper.
Have I simply made a dumb mistake in the pattern?
(Note: I can't just put the uppercase in the web.xml, I need the lowercase value when I start implementing the RollingFileAppender entries.)

Coloured log output is not reflecting in log4j 2 after providing in xml

i have configured the below xml to get the coloured output in console, but still not getting the coloured o/p
<PatternLayout pattern="%highlight{%d %-5p [%t] (%F:%L) - %m%n}{FATAL=red,ERROR=red, WARN=yellow bold, INFO=red, DEBUG=green bold, TRACE=blue}"/>

log4j2 and slf4j/logback differ in output in anylogic

I inherited an anylogic project where parts of the model logic have been implemented in a java project, from which this model is also launched.
This project has multiple logger configurations since in most parts of the project log4j2 is used, in few parts it is slf4j which uses the logback backend.
In the anylogic UI/Browser-Window, you can view some Console in which some output is printed:
Interestingly there are no logs printed, that haven been recorded with the log4j-library.
Those beeing recorded with slf4j and logback however, are also shown in the Console.
The appender configurations read like this:
# log4j2 appender
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
<!--- logback.xml appender --->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%highlight(%d{yyyy.MM.dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}:%L - %msg%n)</pattern>
</encoder>
</appender>
Now for the question: What is difference between the two logging frameworks/appenders, that can cause the observed behavior?
It seems, the SYSTEM_OUT is reassigned after configuration.
If I set the Log4J2 Console Appender to follow
appender.console.follow = true
The log is also appended to the console in the AnyLogic UI.
Apparently logback follows by default.

Lo4j config - Log to different files based on type of mesasge prefix

Hello I am using Log4j2 for my logging and I am little confused as to how I can log message to a different file based on the message prefix.
For example, currently all messages are logged in a single logs folder.
A set of my messages look like this:
'com.project.latency: ProjectName=[MooPointProject].......'
Some of my other log messages are of the format:
'com.project.latency: ProjectName=[DataPlaneProject].......'
I want to log the messages which contain the MooPointProject in a specific file and one containing DataPlaneProject in a separate log file.
Is there a specific way I can do that other than changing the logging level itself?
Set up several appenders one for each project name. For example:
<RollingFile name="RollingFile" fileName="logs/MooPointProject.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<RegexFilter regex=".*MooPointProject.*" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
and
<RollingFile name="RollingFile" fileName="logs/DataPlaneProject.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<RegexFilter regex=".*DataPlaneProject.*" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
Then assign both the appenders to your logger(s). Since your RegexpFilter accepts only a project-spcific lines you will get them to the appropriate files separately.
P.S. - Find the detail on other types of filters here.

Log4j2 RollingFile with daily rotation

Using Log4j2 2.8, is there a way to have my logs rotate daily but so that the current file has a constant name?
Example:
my.log <-- current day (2017-02-13)
my-2017-02-12.log <-- last log
my-2017-02-11.log <-- second to last log
I tried the following configuration without success:
<RollingFile name="RollingFileMain" fileName="my.log"
filePattern="my-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>%level{length=1} %d{yyyy-MM-dd HH:mm:ss} %c{-3} (%F:%L) %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy max="2"/>
</RollingFile>
Additionally, I get the following exception when I launch my application for the first time on a given day:
2017-02-14 09:28:38,334 main ERROR An exception occurred processing Appender RollingFileMain java.lang.NullPointerException
at org.apache.logging.log4j.core.appender.rolling.AbstractRolloverStrategy.getEligibleFiles(AbstractRolloverStrategy.java:107)
at org.apache.logging.log4j.core.appender.rolling.AbstractRolloverStrategy.getEligibleFiles(AbstractRolloverStrategy.java:96)
at org.apache.logging.log4j.core.appender.rolling.AbstractRolloverStrategy.getEligibleFiles(AbstractRolloverStrategy.java:89)
at org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy.purgeAscending(DefaultRolloverStrategy.java:212)
at org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy.purge(DefaultRolloverStrategy.java:199)
at org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy.rollover(DefaultRolloverStrategy.java:326)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.rollover(RollingFileManager.java:310)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.rollover(RollingFileManager.java:227)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.checkRollover(RollingFileManager.java:207)
at org.apache.logging.log4j.core.appender.RollingFileAppender.append(RollingFileAppender.java:267)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:448)
at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:433)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:417)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:403)
at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2091)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:1988)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1960)
at org.apache.logging.log4j.spi.AbstractLogger.info(AbstractLogger.java:1297)
at my.package.MainKt.main(Main.kt:48)
Please change your filepattern to filePattern="my-%d{yyyy-MM-dd}.log":
The %i is only useful when you use the SizeBasedTriggeringPolicy. If you only have a TimeBasedTriggeringPolicy, Log4j gets confused by the %i...
The issue seems to be a combination of #Remko Popma's answer and a bug in Log4j2 2.8. After applying the filePattern="my-%d{yyyy-MM-dd}.log" and downgrading to version 2.7, everything works as expected.
I submitted an issue regarding the crash here: https://issues.apache.org/jira/browse/LOG4J2-1815

Resources