Home java Logging using Maven

Logging using Maven

Author

Date

Category

I’m trying to connect the logging, use only dependence, with the installation of libraries on the big problems started with Lomboka, with him, neither of which did not work, and now would like to see it in any way to work, here is my pomnik:

& lt; dependency & gt;
  & Lt; groupId & gt; org.projectlombok & lt; / groupId & gt;
  & Lt; artifactId & gt; lombok & lt; / artifactId & gt;
  & Lt; version & gt; 1.18.10 & lt; / version & gt;
  & Lt; scope & gt; provided & lt; / scope & gt;
& Lt; / dependency & gt;
& Lt; -! Https://mvnrepository.com/artifact/org.slf4j/slf4j-api - & gt;
& Lt; dependency & gt;
  & Lt; groupId & gt; org.slf4j & lt; / groupId & gt;
  & Lt; artifactId & gt; slf4j-api & lt; / artifactId & gt;
  & Lt; version & gt; 1.7.30 & lt; / version & gt;
& Lt; / dependency & gt;
& Lt; dependency & gt;
  & Lt; groupId & gt; org.apache.logging.log4j & lt; / groupId & gt;
  & Lt; artifactId & gt; log4j-core & lt; / artifactId & gt;
  & Lt; version & gt; 2.13.3 & lt; / version & gt;
& Lt; / dependency & gt;
& Lt; dependency & gt;
  & Lt; groupId & gt; org.apache.logging.log4j & lt; / groupId & gt;
  & Lt; artifactId & gt; log4j-slf4j-impl & lt; / artifactId & gt;
  & Lt; version & gt; 2.13.3 & lt; / version & gt;
& Lt; / dependency & gt;

in the class adds a variable:

private static final Logger logger = LoggerFactory.getLogger (WindowFunction.class);

IDE tightens imports:

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

Here’s an entry in the log:

logger.info ( "start");

All the OK, no errors, but the message is not displayed. Created the file log4j.properties , but I understand that for the output it is not necessary to the console:

log4j.rootLogger = INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.threshold = INFO
log4j.appender.file.File = log / Log.txt
log4j.appender.file.MaxFileSize = 5MB
log4j.appender.file.MaxBackupIndex = 6
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern =% - 5p% c {1}:% L -% m% n
# Direct log messages to stdout
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.threshold = INFO
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =% d {yyyy-MM-dd HH: mm: ss}
log4j.logger.org.springframework = ERROR
log4j.logger.org.hibernate = ERROR
log4j.logger.org.apache.cxf = ERROR

Anyway, neither of which is not displayed.


Answer 1

You are using Log4j 2 . Accordingly, you must specify a log4j2.xml , or log4j2.properties .

Here is an example log4j2.properties (should be src / main / resources ):

# Print configuration errors only
status = error
# Console appender
appender.console.type = Console
appender.console.name = LogToConsole
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [% - 5level]% d {yyyy-MM-dd HH: mm: ss.SSS} [% t]% c {1} -% msg% n
# Root logger
rootLogger.level = info
rootLogger.appenderRef.console.ref = LogToConsole

Answer 2

It is necessary to register the settings in the file resources / log4j.xml , like this:

& lt; DOCTYPE log4j:! Configuration SYSTEM "log4j.dtd" & gt;
& Lt; log4j: configuration
    xmlns: log4j = "http://jakarta.apache.org/log4j/" & gt;
  & Lt; appender name = "console" class = "org.apache.log4j.ConsoleAppender" & gt;
    & Lt; param name = "Target" value = "System.out" / & gt;
    & Lt; layout class = "org.apache.log4j.PatternLayout" & gt;
      & Lt; param name = "ConversionPattern" value = "% d {yyyy-MM-dd HH: mm: ss}% -5p% c {1}:% L -% m% n" / & gt;
    & Lt; / layout & gt;
  & Lt; / appender & gt; 
& lt; Appender name = "FileApander" class = "org.apache.log4j.rollingfileAptender" & gt;
    & lt; param name = "file" value = "mylogs.log" / & gt;
    & lt; layout class = "org.apache.log4j.patternlayout" & gt;
      & lt; param name = "conversionpattern" value = "% d {yyyy-mm-dd hh: mm: ss}% -5p% C {1}:% L -% m% n" / & gt;
    & lt; / layout & gt;
  & lt; / appender & gt;
  & lt; root & gt;
    & lt; Priority Value = "Debug" & gt; & lt; / priority & gt;
    & lt; Appender-Ref ref = "Console" & gt; & lt; / appender-ref & gt;
    & lt; Appender-Ref ref = "FileApnder" & gt; & lt; / appender-ref & gt;
  & lt; / root & gt;
& lt; / log4j: configuration & gt;

Pre-in Maven Register the Resources directory as resources:

& lt; resources & gt;
    & lt; Resource & gt;
      & lt; Directory & GT; SRC / Resources & LT; / Directory & GT;
    & lt; / resource & gt;
  & lt; / Resources & gt;

Answer 3

earned the conclusion in the console after fixed the hub:

& lt; dependency & gt;
  & lt; groupid & gt; ch.qos.logback & lt; / groupid & gt;
  & lt; artifactid & gt; logback-classic & lt; / artifactid & gt;
  & lt; version & gt; 1.2.3 & lt; / version & gt;
& lt; / dependency & gt;
& lt; dependency & gt;
  & lt; groupid & gt; org.slf4j & lt; / groupid & gt;
  & lt; artifactid & gt; SLF4J-API & LT; / artifactid & gt;
  & lt; version & gt; 1.7.28 & lt; / version & gt;
& lt; / dependency & gt;
& lt; dependency & gt;
  & lt; groupid & gt; org.slf4j & lt; / groupid & gt;
  & lt; artifactid & gt; SLF4J-LOG4J12 & LT; / artifactid & gt;
  & lt; version & gt; 1.7.28 & lt; / version & gt;
& lt; / dependency & gt;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions