简体   繁体   中英

How to print log statements in a single line using Log4J?

I have a maven application which contains the logger configured as below.

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();
        }
    }
}

This is my logger.properties file:

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

When I run the program I don't see any errors and the logs are coming out fine. But they are being printed in two lines as shown below:

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

Project Structure:

在此处输入图像描述

Is there any way I can print the log in a single statement & also can we print just the class name instead of full package & class name:

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

You can alter the pattern of your logger with ConversionPatterns in the settings of Log4J: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html This helps you achieve more customized log messages and you can experiment with what the options are and what you would like to see.

  1. Rename your log properties file from 'logger.properties' to 'log4j.properties'.
  2. To output only class name use %C{1} instead of %c{1} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM