P
P
Pavel Dudenkov2016-04-13 19:48:50
Java
Pavel Dudenkov, 2016-04-13 19:48:50

About logback.xml - is it possible to specify in which package which appender to use?

Hello,
In terms of logging, my application is cut into 2 planes:
1 - local application logs
2 - logs of changes that this application makes to the database
So. According to clause 2, logs should be written to the database, respectively, they have a different logic, their own appender with a cache to write in batches, etc.
Well, the question arises, is it possible to somehow specify in the config file - here, in this class or package - I want to use this appender for the database, and in these packages - another appender?
If possible, for example the config:

<configuration>
    <appender name="DB_APPENDER" class="com.test.logging.LogCachedAppender">
    </appender>
    <root level="debug">
        <appender-ref ref="DB_APPENDER" />
    </root>
</configuration>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kornachev, 2016-04-13
@viperz

I do like this:

<configuration debug="false">
   
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%-50class{50} [%-5level]:          %msg%n</Pattern>
        </layout>
    </appender>
    
    <appender name="gui" class="logback.appender.JavaFXAppender">
        <layoutPattern>[%-5level]:    %msg%n</layoutPattern>
    </appender>

    <!-- в коде я запрашиваю это логгер по имени, у него добавлен свой аппендер, все что передается в этот логгер - добавляется в добавленные аппендеры.
         если удалить этот логгер, то все, что было адресовано ему - пойдет в root
 -->
    <logger name="cardservices" level="debug"  additivity="false">
        <appender-ref ref="gui" />
    </logger>

    <root>
         <appender-ref ref="console" />
    </root>


</configuration>

For the desired package and all classes in it, you can give the logger a specific permanent name. And then just add a logger with this name and a description of its appenders to the config.
In the required package, I request to initialize the logger (using the slf4j wrapper, if you do not use it, initialize using the logback methods):
private static Logger logger = LoggerFactory.getLogger("cardservices");

...
//сообщение пойдет в логгер с именем "cardservices" и, соответственно в его аппендеры (для данного примера аппендер "gui".
logger.info("rere");

In your case it will be something like this:
<configuration>
    <appender name="DB_APPENDER" class="com.test.logging.LogCachedAppender">
    </appender>

    <logger name="DB_LOGGER" level="debug"  additivity="false">
        <appender-ref ref="DB_APPENDER" />
    </logger>

    <root level="debug">
        <appender-ref ref="OTHER_APPERNDER" />
    </root>
</configuration>

and initialize the logger in the required package:
private static Logger logger = LoggerFactory.getLogger("DB_LOGGER");

P
Pavel Dudenkov, 2016-04-13
@viperz

Well, or in general, any way I can tie a specific logger only to my DB_APPENDER

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question