Log4j doesn't display process PID - java

I just installed and configured log4j.
It works, but doesn't show process PID, instead prints ${sys:PID} instead of, for example, 12941.
My log4j-config:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout
disableAnsi="false"
pattern="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} %highlight{${LOG_LEVEL_PATTERN:-%5p}}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=green, DEBUG=green bold, TRACE=blue} %style{${sys:PID}}{magenta} --- [%15.15t] %style{%-40.40C{1.}}{cyan} : %m%n%ex"/>
</Console>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
And
console output
So, how can I fix this?

I replaced sys:PID with %pid and it started working.
I took that from github issue

Related

I changed spring boot app to spring mvc but my log4j2.xml is not working?

I change spring boot to spring MVC, but I can't see any log information in my log file but it populates in the console. Please let me know the changes I need to make in the log4j2.xml file so that log info will be saved to log files.
what is monitorinterval=30 in the below XML file?
Thank you soo much for the response
<?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 ${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>
<!-- Rolling File Appender -->
<RollingFile name="FileAppender" fileName="logs/prism.log"
filePattern="logs/prism-%d{yyyy-MM-dd}-%i.log.zip">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.heymath" level="debug"
additivity="false">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="FileAppender"/>
</Logger>
<Root level="info">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="FileAppender"/>
</Root>
</Loggers>
</Configuration>
monitorInterval=30 tells Log4j2 to check every 30 seconds to see if the logging configuration has changed and reconfigure if it has.
I see nothing obviously wrong with your configuration. It should be logging all info, warn, error and fatal messages to the file and console. Debug messages from com.heymath loggers should be going there as well.
I would suggest changing the status="WARN" to status="DEBUG" on the configuration element. If you do not see logs from log4j it means that your configuration file was not found and is not being used. Instead, it is using the default configuration which logs errors to the console.

Same log4j2.xml for different java processes with separate log level

Problem
I want to standardize my log4j2.xml setup for several Java processes that I run on different linux servers. My hard condition: a single log4j2.xml for all servers.
Sometimes it is needed that I change my loglevel from INFO to DEBUG while the Java process is running. You can do this by changing the logLevel in the log4j2.xml, while you have monitorInterval configured.
However, this changes the loglevel of ALL processes that are running on that server! I need a solution that I can change the logLevel for a single process.
My first try
I thought that I would move the logLevel to the properties. Suppose I have the log4j2.xml (as an example):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Configuration monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %t %-5p %-32c{1} - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="${bundle:logger:logLevel}">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Next to this, I also have the file logger.properties on the classpath:
logLevel=INFO
Now when I change my log4j2.xml (by just adding a space somewhere) and updating this property file, the logging changes. However, as mentioned it changes the logLevel of ALL processes.
What I want
I want to be able to have a logger.properties that looks like this:
process1LogLevel=INFO
process2LogLevel=DEBUG
And then something in the log4j2.xml that configures this property dynamically:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Configuration monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %t %-5p %-32c{1} - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="${bundle:logger:<something based on the process name>}">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
However, this "flexible" property doesn't really work. Do you have any idea for a solution?
Note
If you have a different idea that looks a little bit different but achieves my goals, that is also fine! Any out-of-the-box solution is always appreciated :)
Found it. You can nest properties. If you specify -Dname=process1 for example, you can use it like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Configuration monitorInterval="1">
<Properties>
<Property name="process1">DEBUG</Property>
<Property name="process2">INFO</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %t %-5p %-32c{1} - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="${${sys:name}}">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

Issue in log4j2 Custom Filter

I am trying to implement custom filter as mentioned in post Log4j2 custom filter .My lo4j2.xml is as follows
<?xml version="1.0" encoding="UTF-8"?>
<!-- Logger configuration when running outside of docker -->
<Configuration status="trace" packages=“com.rest.server_common.logging">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c{1}:%L - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="DEBUG">
<com.rest.server_common.logging.MyCustomFilter level="DEBUG"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
When I start my jetty server,I get the error
main ERROR Root contains an invalid element or attribute "com.rest.server_common.logging.MyCustomFilter"
What is the problem here?
I have even tried moving filter line
<com.rest.server_common.logging.MyCustomFilter level="DEBUG"/>
after Configuration element but I still get the error
Just to close.The package was in different module so the filter was not getting recognised.

No console output using log4j2 and slf4j

am I missing something using this configuration? I thought that root logger would log all events from error level to the bottom one. If I try to log event at info level, there is no output. This config gives me only error leveled logs. I am using slf4j to create logger objects through Logger-factory class.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
For info level logs, change the configuration, in particular Root level to info as show below
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
By changing level to info, the program will start logging both info, error logs in the console.

Log4j2 - create new log file

I have configuration for save logs to file, but logs are append to existing content. I want to create always new file. How can I do that?
My log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="File" fileName="out.log">
<PatternLayout pattern="[%d{ISO8601} %-5level] %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="File"/>
</Root>
<Root level="info">
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
The FileAppender has a property named append which has a default value of true, so configure it like this:
<File name="File" fileName="out.log" append="false">
Documentation can be found at https://logging.apache.org/log4j/2.x/manual/appenders.html#FileAppender

Categories

Resources