Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging is processed by the logger twice

I have a web-application which I want to log for. There is some default logging provided by a logging.properties file, which I am unable to influence. It logs the server startup, shutdown, etc.

These are examples of such logs (normal ones, nothing wrong here):

2016/01/22 12:25:10  INFO (ApplicationContext.java:671 [ServerService Thread Pool -- 64]) - Initializing Spring root WebApplicationContext
2016/01/22 12:25:13  INFO (ConfigureListener.java:202 [ServerService Thread Pool -- 64]) - Initializing Mojarra 2.1.28-jbossorg-6  for context '/MyServices'
2016/01/22 12:25:15  INFO (DeploymentHandlerUtil.java:137 [ServerService Thread Pool -- 28]) - JBAS015859: Deployed "MyServices.war" (runtime-name : "MyServices.war")
2016/01/22 12:25:15  INFO (BootstrapListener.java:93 [Controller Boot Thread]) - JBAS015874: JBoss EAP 6.4.0.GA (AS 7.5.0.Final-redhat-21) started in 27257ms - Started 485 of 520 services (63 services are lazy, passive or on-demand)

Now I also have logging in my application for my own methods, specified using:

private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(PropertiesValues.class);
...
LOG.info("Properties loaded.");

The problem is that when these logs are hit, the causes double logging, where each line has two timestamps, two log levels, and two sources, such as:

2016/01/22 11:50:49  INFO (AbstractLoggingWriter.java:71 [ServerService Thread Pool -- 111]) - 2016/01/22 11:50:49  INFO (EnvironmentLoader.java:128 [ServerService Thread Pool -- 111]) - Starting Shiro environment initialization.
2016/01/22 11:50:49  INFO (AbstractLoggingWriter.java:71 [ServerService Thread Pool -- 111]) - 2016/01/22 11:50:49  INFO (EnvironmentLoader.java:141 [ServerService Thread Pool -- 111]) - Shiro environment initialized in 125 ms.
...
2016/01/22 11:51:20  INFO (AbstractLoggingWriter.java:71 [http-localhost/127.0.0.1:8443-6]) - 2016/01/22 11:51:20  INFO (PropertiesValues.java:101 [http-localhost/127.0.0.1:8443-6]) - Properties loaded.

Note the logs are being processed twice. That is - Each line that would be printed is run through the logger again, resulting in two timestamps, two log levels, and two sources. I can get around this by changing the last line in my log4j.properties file:

log4j.rootLogger=INFO, STDOUT
log4j.additivity.rootLogger=false
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.Target=System.out
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} %5p (%F:%L [%t]) - %m%n

To only be %m but I feel that this is a poor solution, and results in the source being shown as AbstractLoggingWriter.java:71 for everything.

Is there a way to make it so that my logs show the correct method and line number, and are formatted how I choose, rather than the server? At the same time, I also don't want to lose the logging provided by the server.

My Maven dependencies are:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
like image 705
Addison Avatar asked Jan 22 '16 03:01

Addison


People also ask

Why does logger print twice in Python?

If you use the standard construction logger = logging. getLogger('mymodule') and then accidentally mistype loggger as logging i.e. then this will cause duplicate messages to come up because the call to logging creates a new logger.

Does logging work with multiprocessing?

The multiprocessing module has its own logger with the name “multiprocessing“. This logger is used within objects and functions within the multiprocessing module to log messages, such as debug messages that processes are running or have shutdown. We can get this logger and use it for logging.

What is logging method?

The main logging methods that can be used for the quantitative evaluation of gas-bearing properties include (1) using sealed coring data to quantitatively determine the original gas saturation in gas zones, (2) using the total-hydrocarbon analysis data from gas logging to obtain hydrocarbon gas contents, (3) using the ...

What is logger logging getLogger (__ name __)?

getLogger(name) is typically executed. The getLogger() function accepts a single argument - the logger's name. It returns a reference to a logger instance with the specified name if provided, or root if not. Multiple calls to getLogger() with the same name will return a reference to the same logger object.


2 Answers

We had the same problem with our application, we fixed that by adding the following in jboss-deployment-structure.xml to WEB-INF in the web application:

<?xml version="1.0" encoding="utf-8"?>
<jboss-deployment-structure>
   <deployment>
       <!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->
       <exclude-subsystems>  
           <subsystem name="logging" />
       </exclude-subsystems>  
   </deployment>
</jboss-deployment-structure>
like image 66
SunSear Avatar answered Nov 19 '22 08:11

SunSear


It's hard to say exactly what's happening, but I'll try to guess. I see you use JBoss, it uses commons-logging to log events. You set log4j to output to stdout. Possibly Logger from JBoss grabs what's being sent to stdout and logs it, so you have a message decorated twice.

Try to use org.jboss.logging.Logger from JBoss instead of slf4j Or as a dirty trick you may try to switch off log4j output by adding log4j.appender.STDOUT.Threshold=off to log4j.properties

like image 1
Dmitry P. Avatar answered Nov 19 '22 06:11

Dmitry P.