简体   繁体   中英

How to stop log4j from logging

NB While I have not managed to work out the final solution to the issue, with some help from Andreas I was able to realise that I had configured log4j incorrectly and it was using a default properties file and not the one that I specified myself. I will post the solution when I have it working properly so that it is available for people in the future... right after octoberfest this weekend :D

Okay, so I know that similar questions have been asked on here, however I have yet to find a post that has this same problem or a solution to fix it.

Okay, so I tried to start using log4j to try and get used to proper logging with my code (yay best practices!) but I ran into a bit of an issue while trying to fix an error in my code. The problem is that I need to read the console output, but log4j is putting everything there (DEBUG). At first this wasn't a problem so I didn't bother to get log4j to stop doing it. But now I am finding it quite hard to look for the information that I need in the output.

As a quick solution to my problem, I tried to programmatically change the logging level to OFF on both my loggers, nope.

Then I tried to change the properties file to prevent it writing to the console, nope.

Then I tried to remove everything in the properties file thinking that log4j would no longer know how to behave, nope.

Then I tried to completely remove log4j from my code by commenting out all of the lines and not initialising any loggers, nope... wait, what?

Yep, it seems that even without any log4j code in my project, log4j just does not want to stop.

At this point I tried to clean the project (I'm using eclipse), then I tried to restart eclipse, and then my laptop.

I know that I am probably making a massive oversight, but I feel like commenting out ALL OF THE CODE would do something.

I should note that NOTHING I tried made any difference to the content being printed, it always printed the same lines in the same order (hence why I am sure that I am doing something wrong)

Any help that can be offered would be greatly appreciated

EDIT 1: I should also state that the reason that I think it is log4j causing the issue is that all of the output in the console is taking the format that I defined in the log4j.properties file.

EDIT 2:

one class

public class MessageSender extends AbstractCommand{
//private static final Logger LOG = Logger.getLogger(HelloWorldCommandFactory.class);

private String messageText = "placeholder";
private final long MESSAGE_ID = XXXXXXXXXX;

@Override
public void execute() {
    try {
        //LOG.setLevel(Level.OFF);
        //LOG.info("MESSAGE_TEXT: "+messageText);                                                                   
        TelegramRequest telegramRequest = TelegramRequestFactory.createSendMessageRequest(MESSAGE_ID, messageText, true, null, null);
        //LOG.info("REQUEST: "+telegramRequest.equals(getRequestHandler()));
        requestHandler.sendRequest(telegramRequest);
    } catch (JsonParsingException | TelegramServerException e) {
        e.printStackTrace();
    }
}

public MessageSender(Message message, RequestHandler requestHandler){
    super(message, requestHandler);
}

public MessageSender(Message message, RequestHandler requestHandler, String text){
    super(message, requestHandler);
    this.messageText = text;
}

the other class

public class MyCommandFactory implements CommandFactory {

    //private static final Logger LOG = Logger.getLogger(MyCommandFactory.class);

    @Override
    public Command createCommand(Message message, RequestHandler requestHandler) {
        //LOG.setLevel(Level.OFF);
        //LOG.info("MESSAGE_ID: "+message.getChat().getId()+"  MESSAGE_BODY: "+message.getText());
        return new MessageSender(message, requestHandler);
    }
}

sample of output (I have removed certain sensitive information)

15-09-25 06:32:42 INFO  AbstractCommandDispatcher:69 - *************************************
2015-09-25 06:32:42 INFO  AbstractCommandDispatcher:70 - ** Starting up command dispatcher...
2015-09-25 06:32:42 INFO  AbstractCommandDispatcher:71 - *************************************
2015-09-25 06:32:43 INFO  AbstractCommandWatcher:50 - >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2015-09-25 06:32:43 INFO  AbstractCommandWatcher:51 - >> Starting up command watcher...
2015-09-25 06:32:43 INFO  AbstractCommandWatcher:52 - >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
2015-09-25 06:32:45 DEBUG DefaultCommandWatcher:104 -   Polling Telegram updates (offset:0, limit:100, timeout=0)...
2015-09-25 06:32:45 DEBUG RequestAddCookies:122 - CookieSpec selected: default
2015-09-25 06:32:45 DEBUG RequestAuthCache:76 - Auth cache not set in the context
2015-09-25 06:32:45 DEBUG PoolingHttpClientConnectionManager:249 - Connection request: [route: {s}->https://api.telegram.org:443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
2015-09-25 06:32:45 DEBUG PoolingHttpClientConnectionManager:282 - Connection leased: [id: 0][route: {s}->https://api.telegram.org:443][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
2015-09-25 06:32:45 DEBUG MainClientExec:234 - Opening connection {s}->https://api.telegram.org:443
2015-09-25 06:32:46 DEBUG DefaultHttpClientConnectionOperator:131 - Connecting to api.telegram.org/
2015-09-25 06:32:46 DEBUG SSLConnectionSocketFactory:335 - Connecting socket to api.telegram.org/ with timeout 0
2015-09-25 06:32:46 DEBUG SSLConnectionSocketFactory:388 - Enabled protocols: [TLSv1, TLSv1.1, TLSv1.2]
2015-09-25 06:32:46 DEBUG SSLConnectionSocketFactory:389 - Enabled cipher suites:[TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, TLS_RSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_DSS_WITH_AES_128_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_DSS_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA, SSL_RSA_WITH_RC4_128_SHA, TLS_ECDH_ECDSA_WITH_RC4_128_SHA, TLS_ECDH_RSA_WITH_RC4_128_SHA, SSL_RSA_WITH_RC4_128_MD5, TLS_EMPTY_RENEGOTIATION_INFO_SCSV]
2015-09-25 06:32:46 DEBUG SSLConnectionSocketFactory:393 - Starting handshake
2015-09-25 06:32:46 DEBUG SSLConnectionSocketFactory:423 - Secure session established
2015-09-25 06:32:46 DEBUG SSLConnectionSocketFactory:424 -  negotiated protocol: TLSv1.2
2015-09-25 06:32:46 DEBUG SSLConnectionSocketFactory:425 -  negotiated cipher suite: TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
2015-09-25 06:32:46 DEBUG SSLConnectionSocketFactory:433 -  peer principal: CN=api.telegram.org, OU=Domain Control Validated
2015-09-25 06:32:46 DEBUG SSLConnectionSocketFactory:442 -  peer alternative names: [api.telegram.org, www.api.telegram.org]
2015-09-25 06:32:46 DEBUG SSLConnectionSocketFactory:446 -  
2015-09-25 06:32:46 DEBUG DefaultHttpClientConnectionOperator:138 - Connection established 
2015-09-25 06:32:46 DEBUG MainClientExec:255 - Executing request POST 
2015-09-25 06:32:46 DEBUG MainClientExec:260 - Target auth state: UNCHALLENGED
2015-09-25 06:32:46 DEBUG MainClientExec:266 - Proxy auth state: UNCHALLENGED
2015-09-25 06:32:46 DEBUG headers:124 - http-outgoing-0 >> 
2015-09-25 06:32:46 DEBUG headers:127 - http-outgoing-0 >> Content-Length: 28
2015-09-25 06:32:46 DEBUG headers:127 - http-outgoing-0 >> Content-Type: application/x-www-form-urlencoded; charset=UTF-8
2015-09-25 06:32:46 DEBUG headers:127 - http-outgoing-0 >> Host: api.telegram.org

EDIT 3:

log4j.properties file

log4j.rootLogger=INFO, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= %d{HH:mm:ss,SSS} [%t] %-5p %x %C{1} : %m%n

You can change the level to OFF which should get rid of all logging. According to the log4j website, valid levels in order of importance are TRACE , DEBUG , INFO , WARN , ERROR , FATAL . There is one undocumented level called OFF which is a higher level than FATAL, and turns off all logging.

Are you doing it correctly while taking the programmatic approach? This is how I would do it -

List<Logger> loggers = Collections.<Logger>list(LogManager.getCurrentLoggers());
loggers.add(LogManager.getRootLogger());
for ( Logger logger : loggers ) {
logger.setLevel(Level.OFF);
}

Alternatively , you could also do Logger.getRootLogger().removeAllAppenders(); Logger.getRootLogger().addAppender(new NullAppender()); Logger.getRootLogger().removeAllAppenders(); Logger.getRootLogger().addAppender(new NullAppender()); for complete silence like a command line tool .

Make sure you restart eclipse and clean build your project for the changes to take effect.

If the above doesnt work - Create a log4j.properties file and place that file in the classpath of your application. Paste the following contents in the same -

log4j.debug=FALSE
log4j.rootLogger=INFO, R
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=C:/apache-tomcat-5.5.27/logs/testlog.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss.SSSS} %p %t %c - %m%n

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM