简体   繁体   中英

Get log object in memory - Logback

I'm using logback in spring-boot 2.2.5.RELEASE , I need to get the log object in memory so I can manipulate the info and proccess it.

What I would expect is something like this.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class application {

    Logger logger = LoggerFactory.getLogger(application.class);

    public void executeTask(Integer queryMinutes) {
        logger.info(INICIO_TRANSACCION, metodo);

        try {
            //Do something
            //Log informative messages
        } catch (DBException e) {
            //Log ERROR messages
            logger.error(MENSAJE_EXCEPCION + e, e);
            logger.info(ROLBACK_TRANSACCION);

        } finally {
            //Here I need to call a method to further process the info printed in the log something like
            logger.getMessage();
            logger.getLineNumber();
            logger.getThread();
            callSomeMethod(logger);
            logger.info(TIEMPO_PROCESO, (System.currentTimeMillis() - tiempoInicial));
            logger.info(FIN_TRANSACCION, metodo);
        }
    }
}

I know that when you work with appenders we usually define a ILoggingEvent object and this give access to logger.getMessage() and more,

The question is how to get the log object in my java class so I can access the properties.

The basic design of logback (and log4j) is that it is part of your program but is not visible to the calling code. Hence what you want to do is basically against the spirit of the framework you want to use, which I suggest you reconsider.

The logback term for the component that actually ensures that the destination receives a log event is an "appender". http://logback.qos.ch/manual/appenders.html

The problem is that you want to send a mail using a non-standard API, otherwise you could just have used the standard SMTPAppender ( http://logback.qos.ch/manual/appenders.html#SMTPAppender )

I would suggest either subclassing the SMTPAppender to do what you want, or write a javax.mail.Session object that implements the JavaMail API and provide it to a stock SMTPAppender using JNDI. Or a third thing. Just not in your own part of the code.

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