6

i am using logback for logging and in logback.xml i have Console appender as

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>

              <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
    </appender>.

i am trying to achieve something like this...

time  thread  |-**CUSTOMLOGLEVEL**  xyz.class - Message.

Why ? i want to filter the messages easily by defining the loglevel or some other indicator.

for example: search logs with log level "CUSTOMLOGLEVEL". Is there any way to give custom log level or any other indicator which shows that this is custom generated log and not some framework generated log..

i went into direction of creating custom class .

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
         <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="com.logging.CustomLayout">
                             <param name="argument1" value="1" />
                             <param name="argument2" value="2" />

            </layout>
        </encoder>
    </appender>

but i am not sure how i will give input externally to these parameters.

In case i am not clear please let me know .

lesnar
  • 2,400
  • 7
  • 41
  • 72
  • 3
    You might want to use [Markers](https://stackoverflow.com/questions/16813032/what-is-markers-in-java-logging-frameworks-and-that-is-a-reason-to-use-them) instead of Custom Log Levels – hinneLinks Jun 08 '16 at 06:22

1 Answers1

12

SLF4J/Logback solves the problem of "I want to do something more complex than a log level" with a feature called Markers.

For example, to mark some logs as "interesting":

Marker interesting = MarkerFactory.getMarker("INTERESTING");
Logger logger = LoggerFactory.getLogger(getClass());
…
logger.info(interesting, "Something happened: {}", value)

In the PatternLayout, one can use %marker to record the marker associated with a log entry. (See %marker in the documentation.)

This is similar to what's in a SLF4J FAQ entry on why they don't have a "FATAL" level and how to use Markers.

Another option (since you're specifically asking about filtering to see if it's a "custom generated log and not some framework generated log") is to ensure that your custom logs are all in a logger that's named what you want (maybe starting with com.yourcompany.custom.), and just using the normal logger filtering. While it's often very handy to have loggers be named after the class that they're in, sometimes using a different name for different loggers more accurately represents what you're trying to log, and allows for easy filtering and searching as well.

  • 1
    Using marker I am able to log marked logs into new appender. But do we have an option to avoid logging those marked logs in appender used for rest ? – Karthikaiselvan R Jun 24 '19 at 17:37
  • @KarthikaiselvanR: I think you might want to ask your own new question about the problem you're dealing with, including the specifics about the logback configuration you're trying to use. –  Jun 26 '19 at 12:55