如何使用Log4J在一行中打印日志语句?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Log4J在一行中打印日志语句?相关的知识,希望对你有一定的参考价值。

我有一个Maven应用程序,其中包含配置如下的记录器。

import org.apache.commons.cli.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class StartCount {
    public static void main(String[] args) throws Exception {
        ChunkRecon chRecon;
        Options options = new Options();
        Option input    = new Option("s", "ssn", true, "source system names");
        input.setRequired(false);
        options.addOption(input);
        CommandLineParser parser = new DefaultParser();
        HelpFormatter formatter  = new HelpFormatter();
        CommandLine cmd          = null;
        CustomSsnTableRecon cstr;
        final Logger LOGGER = Logger.getLogger(StartCount.class.getName());
        try {
            cmd = parser.parse(options, args);
            if(cmd.hasOption("s")) {            // Checks if there is an argument '--s' in the CLI.
                String sources = cmd.getOptionValue("s");
                if(sources == null) {
                    System.out.println("Please enter a source system name using the option '--s' and submit the jar again.. Ex: --s ABC ");
                    System.exit(1);
                }
                if(sources.contains("custom")) {
                    cstr = new CustomSsnTableRecon(sources);
                    cstr.getChunks();
                    cstr.reconDataFile();
                } else {
                    LOGGER.log(Level.INFO, "Starting recon for the source: " + sources);
                    chRecon = new ChunkRecon(sources);
                    chRecon.getChunks();
                    chRecon.reconDataFile();
                }
            } else {
                LOGGER.log(Level.WARNING, "Please enter a source system using the option '--s' and submit the jar again. Ex: --s ABC");
                System.out.println("Please enter a source system name using the option '--s' and submit the jar again.. Ex: --s ABC ");
                System.exit(1);
            }
        } catch(ParseException e) {
            LOGGER.log(Level.WARNING, "Please enter a source system using the option '--s' and submit the jar again. Ex: --s ABC");
            formatter.printHelp("utility-name", options);
            e.printStackTrace();
            System.exit(1);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

这是我的logger.properties文件:

log4j.rootLogger=DEBUG, INFO, WARN, ERROR, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
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} %-5p %c{1}:%L - %m%n

当我运行程序时,我没有看到任何错误,并且日志显示的很好。但是它们以两行打印,如下所示:

Nov 18, 2019 12:30:22 PM com.recordcount.entry.StartCount main
INFO: Starting recon for the source: GCORP_HFM

项目结构:

enter image description here

有什么方法可以在单个语句中打印日志,也可以仅打印类名而不是完整的包和类名:

Nov 18, 2019 12:30:22 PM StartCount main INFO: Starting recon for the source: GCORP_HFM
答案

您可以在Log4J的设置中使用ConversionPatterns更改记录器的模式:https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html这可以帮助您获得更多自定义的日志消息,并且可以尝试使用哪些选项以及您希望看到的内容。

另一答案
  1. 将您的日志属性文件从'logger.properties'重命名为'log4j.properties'。
  2. 仅输出类名,请使用%C{1}代替%c{1}

以上是关于如何使用Log4J在一行中打印日志语句?的主要内容,如果未能解决你的问题,请参考以下文章

如何将Hibernate生成的SQL语句打印到日志文件

如何设置hibernate打印sql语句

org.apache.log4j.Logger用法

log4j.properties日志级别debug打印后台sql语句

怎么配置log4j 打印出sql语句

Java项目中如何使用log4j和slf4j实现日志打印