Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java logging with log4j and log4j.yaml

Looks like java developers are avoiding logging with log4j.yaml they prefer to use log4j.xml instead

I know that there is a support for this in log4j v2 but do not understand why on internet there are no detailed documentation related to this ?

I found a lot documentation python/ruby logging and yaml. I know that java is old language but not understand why java developers are not interested in new things.

Update:

found out how to do, It was hard ;)

log4j2.yml or log4j2-test.yml

# why yaml http://jessenoller.com/blog/2009/04/13/yaml-aint-markup-language-completely-different

status: WARN
monitorInterval: 900 # 15 min = 900 sec

properties:
    property:
        -
            name: pattern_layout_console
            value: "%d - [%t] %-5p - %c - %M(%L) | %m%n"

        -
            name: pattern_layout_console_no_threads
            value: "%d - %-5p - %c - %M(%L) | %m%n"

        -
            name: log_path
            value: "./logs"

appenders:
    console:
        -
            name: CONSOLE
            PatternLayout:
            pattern: "${pattern_layout_console_no_threads}"

    file:
        -
            name: DEBUG_FILE
            fileName: ${log_path}/debug.log
            PatternLayout:
                pattern: "${pattern_layout_console}"
             append: false

        -
            name: INFO_FILE
            fileName: ${log_path}/info.log
            PatternLayout:
                pattern: "${pattern_layout_console_no_threads}"
            append: false

######## .....    

loggers:
    logger:
        -   
            name: ch.company.module
            additivity: false
            appenderRef:
                -
                    ref: DEBUG_FILE
                    level: DEBUG
                -
                    ref: INFO_FILE
                    level: INFO
                -
                    ref: WARN_FILE
                    level: WARN
                -
                    ref: ERROR_FILE
                    level: ERROR
    root:
        level: INFO
        appenderRef:
              ref: CONSOLE

pom.xml

....
<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
        <version>2.4.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>2.1</version>
    </dependency>
    ... 
like image 499
Andrei.Danciuc Avatar asked Oct 24 '14 14:10

Andrei.Danciuc


People also ask

Can we use log4j in Java?

Log4j can be configured through Java code or in a configuration file. Configuration files can be written in XML, JSON, YAML, or properties file format.

Can we use log4j and Log4j2 together?

Log4j 2 provides support for the Log4j 1 logging methods by providing alternate implementations of the classes containing those methods. These classes may be found in the log4j-1.2-api jar distributed with the project.


1 Answers

The question seems to be "Why are people using XML configuration rather than YAML configuration?"

The answer as @Stephen C suggests is history. Log4j 2 was released in 2014 and added YAML and JSON configuration format support. This means that from 2001 to 2014, your options for log4j configuration files was either XML or properties files (configuration via properties files was removed in log4j 2).

like image 103
krock Avatar answered Sep 22 '22 06:09

krock