No console output using log4j2 and slf4j - java

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.

Related

How to disable log messages when spring boot application run?

This is my log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="basePath">/folderName/logs/</Property>
</Properties>
<Appenders>
<RollingFile name="fileLogger" fileName="${basePath}/fileName.log"
filePattern="${basePath}/edf-info-%d{MM-yyyy-dd}.log">
<PatternLayout>
<pattern>%d{MM-yyyy-dd} [%-5level] %l - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout>
<pattern>%d{MM-yyyy-dd} [%-5level] %l - %msg%n</pattern>
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Logger name="com.company.projectName" level="info" additivity="true">
<appender-ref ref="fileLogger" level="debug"/>
</Logger>
<Root level="info" additivity="false">
<appender-ref ref="console"/>
</Root>
</Loggers>
</Configuration>
When I used previous versions of Log4j2, only requests sent by my application were being written to my log file in my log folder on my computer. For example:
12-2021-27 [INFO ] com.company.projectname.controller.ClassController.methodName(ClassController.java:21) - paramName1, paramname2
12-2021-27 [INFO ] com.company.projectname.service.ClassServiceImpl.methodName(ClassServiceImpl.java:21) - paramName1, paramname2
But when I switched to the new version of Log4j, because of the vulnerability, before the messages I showed above, when my spring boot project runs, its messages also written. I mean focus on following.
12-2021-22 [INFO ] org.springframework.boot.StartupInfoLogger.logStarting(StartupInfoLogger.java:55) - Starting ProjectNameApplication using Java 1.8.0_111 on pc-name with PID 00000 (C:\Workspace\project-name\target\classes started by user.namein C:\Workspace\project-name)
12-2021-22 [INFO ] org.springframework.boot.SpringApplication.logStartupProfileInfo(SpringApplication.java:635) - No active profile set, falling back to default profiles: default
12-2021-22 [INFO ] org.springframework.boot.StartupInfoLogger.logStarted(StartupInfoLogger.java:61) - Started ProjectNameApplication in 8.858 seconds (JVM running for 12.962)
but I don't want to see these messages in my log file. I just want to write messages when request and response are generated, like before my configuration. How to solve this? I mean I want to only messages to the log file which my requests and responses.
Those unwanted messages use SpringApplication#getApplicationLog() to retrieve a logger, whose name is the fully qualified class name of your main class. The logger's name is probably a child of "com.company.projectName", hence it is sent to the file appender. You can confirm it by adding %c to your pattern layout.
Assuming your main class is called com.company.projectName.Application, you can:
either configure a com.company.projectName.Application logger that uses only the console appender:
<Logger name="com.company.projectName.Application" level="INFO" additivity="false">
<AppenderRef ref="console"/>
</Logger>
or replace the com.company.projectName logger with more specific ones:
<Logger name="com.company.projectName.controller" level="DEBUG">
<AppenderRef ref="fileLogger" />
</Logger>
<Logger name="com.company.projectName.controller" level="DEBUG">
<AppenderRef ref="fileLogger" />
</Logger>

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.

Change Quartz appender in log4j2

I'm trying to redirect all Quartz logging to a separate file, but it still keeps logging into the console. What am I doing wrong in the configuration file?
Here's a simplified version of my log4j2.xml
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<!--stuff-->
</Console>
<RollingFile name="Quartz">
<!--stuff-->
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.quartz" level="ALL">
<AppenderRef ref="Quartz"/>
</Logger>
<Logger name="com.rotoplastyc" level="ALL">
<AppenderRef ref="Console" />
</Logger>
<Root level="OFF">
</Root>
</Loggers>
</Configuration>
Found out that, as #teppic suggested in the comments, I needed the log4j-slf4j-impl lib in order for it to work properly, I was currently using the slf4j-simple lib which only logs into the console.

Log4j set a different logging level for each appender

I'm using Log4j 2 to log the events of my application. However I'm stuck at the following problem.
Currently all logging messages are being written to two different appenders. One has RollingFile type, while the other has Console type.
What I want is for the RollingFile appender to log messages with an INFO level or higher (ERROR, FATAL), and for the Console appender to log messages with an ERROR level or higher (FATAL).
Inside my log4j2.xml file I seem to be only able to declare the logging level for an entire logger (including all of its appenders). Here is my log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>%d %level %msg%n</Pattern>
</PatternLayout>
</Console>
<RollingFile name="Log" fileName="log/Log.log" filePattern="log/Log-%d{yyyy-MM-dd}-%i.log" append="false">
<PatternLayout>
<Pattern>%d %level %msg%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="Log" />
</Root>
</Loggers>
</Configuration>
Is there an easy way of doing so? I searched log4j documentation but couldn't find what I was looking for (Maybe I missed it?). If it's possible I would really prefer for the solution to be applicable on any appenders' types; not specific for RollingFile and Console.
EDIT:
I saw many questions where it was asked to write ONLY the messages from a certain level to a file, while writing the messages from a different level to a different file. In my case I need the messages with a certain level of HIGHER to be written to different files. For example in the case I provided messages with level ERROR or FATAL will be written to both the RollingFile and Console, while messages with level INFO will be written to RollingFile only.
To limit logging level on specific appender in log4j2 you should use ThresholdFilter element of an appender.
In your case log4j2.xml file will look like:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<Pattern>%d %level %msg%n</Pattern>
</PatternLayout>
</Console>
<RollingFile name="Log" fileName="log/Log.log" filePattern="log/Log-%d{yyyy-MM-dd}-%i.log" append="false">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<Pattern>%d %level %msg%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="Log" />
</Root>
</Loggers>
</Configuration>
Here is a simple test:
public class Log4jTest {
private static final Logger logger = LogManager.getLogger(Log4jTest.class);
public static void main(String[] args) {
logger.debug("Debug");
logger.info("Info");
logger.warn("Warning");
logger.error("Error");
logger.fatal("Fatal");
}
}
Console output:
2020-02-25 12:33:50,587 ERROR Error
2020-02-25 12:33:50,589 FATAL Fatal
Log.log contents:
2020-02-25 12:33:50,585 INFO Info
2020-02-25 12:33:50,587 WARN Warning
2020-02-25 12:33:50,587 ERROR Error
2020-02-25 12:33:50,589 FATAL Fatal
In first version of log4j it was Threshold property of appender. On how to solve the same in log4j see the answer on question.
With aid of filters Log4j2 allows to configure the output to a specific appender much more flexible then log4j.
This should do the trick.
<Loggers>
<Root level="debug" additivity="false">
<AppenderRef ref="Console"/>
</Root>
<Logger name="com.project.package" level="info" additivity="false">
<AppenderRef ref="Log" />
</Logger>
</Loggers>
Alternatively, you could do it like this - Different level of logs in different log files
BTW, this is explained very nicely in the Log4j2 documentation. See - https://logging.apache.org/log4j/2.x/manual/configuration.html

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