Skip to content

Logging

Mike Ahlers edited this page Oct 11, 2018 · 6 revisions

Back to home

Logging

Index

Introduction

Every tool needs logging and this tool is no exception.

Initially, the implementation of this mock was using the logback library. This library is dubbed successor of the log4j framework. However, after research I have switched back to log4j again, or more specifically the next generation called log4j2.

After reading that their implementation uses async log appenders, and optimized garbage collection it seems tailor made for high performance, high volume logging. See also: http://logging.apache.org/log4j/2.x/performance.html

Default

Currently there is hardly any logging (only some output showing configuration). This is done by design with performance tests in mind (i.e. no spam). The micro services uses an embedded log configuration (based on log4j2):

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="%date{DEFAULT} [%level] [%thread] [%class{36}] %message%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="net.technolords.micro.filter" level="error" additivity="false">
            <AppenderRef ref="console"/>
        </Logger>
        <Root level="info">
            <AppenderRef ref="console"/>
        </Root>
    </Loggers>
</Configuration>

Custom

It is possible to have your own log configuration, where you have full control over the format and levels. See also: https://logging.apache.org/log4j/2.x/manual/layouts.html

In order to achieve this a property must be specified. For the command line, i.e. using a system property it looks like:

-Dlog4j.configurationFile=log4j2.xml

The value can be relative or an absolute path referring to your log configuration file.

MDC

The micro service also supports some mapped diagnostic context (aka MDC), see also:

Currently supported MDC's (note that these must be wrapped with %X{...}:

Key Remarks
%X{httpUri} The detected http uri, such as /mock/cmd
%X{httpStatus} The returned http status code, such as 200, 404, etc

An example looks like:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="%date{DEFAULT} [%level] [%thread] [%class{36}] %message%n"/>
        </Console>
        <Console name="filterAppender" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{UNIX_MILLIS} %X{httpUri} %X{httpStatus} %message%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="net.technolords.micro.filter" level="info" additivity="false">
            <AppenderRef ref="filterAppender"/>
        </Logger>
        <Root level="info">
            <AppenderRef ref="console"/>
        </Root>
    </Loggers>
</Configuration>

Note the Logger is used with additivity false (this will prevents double log entries).

Using the configuration above, it will produce output that looks like:

1476992980855 /mock/cmd 200 16
1476992981586 /mock/cmd 200 1
Clone this wiki locally