-1

I would like to change my log format.

What I have:

Jul 23, 2019 10:17:02 PM myclass log INFO: message

What I want:

Jul 23, 2019 10:17:02 INFO: message

how can I do that, can you help me?

public class AIVLogger {

    private static Logger logger = Logger.getLogger(AIVLogger.class.getName());
    private static FileHandler fh = null;
    private static String logFolder = System.getProperty("user.dir") + File.separator + "Configuration_Data" + File.separator + "log";
    private static String logFile = logFolder + File.separator + "logger";

    public static void initialize() {

        try {
            new File(logFolder).mkdirs();
            fh = new FileHandler(logFile + "_" + GenericFuncs.getTimeNowFileName() + ".txt", false);
        } catch (SecurityException e) {
            AIVLogger.log(e.getMessage());
        } catch (IOException e) {
            AIVLogger.log(e.getMessage());
        }
        fh.setFormatter(new SimpleFormatter());
        logger.addHandler(fh);
        logger.setLevel(Level.ALL);
    }

    public static Logger getInstance() {
        return logger;
    }

    public static void log(String logMessage) {
        //logger.log(logger.getLevel(), logMessage);
        Level x = logger.getLevel();
        logger.log(x, logMessage);
    }

}

This is the class that I'm trying to change.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Have you created the class `FileHandler` or is it from a library? – deHaar Jul 23 '19 at 10:04
  • It´s from a library. I would like to @override the 'toString' method. – Afonso Marques Jul 23 '19 at 10:12
  • You will have to inherit from that class if it is from a library and you want to override `toString()`. – deHaar Jul 23 '19 at 10:14
  • But how can I edit the logging format ? – Afonso Marques Jul 23 '19 at 10:17
  • Please add some example (Java code, not log output) where you use the class `FileHandler`. Is it `java.util.logging.FileHandler`? – deHaar Jul 23 '19 at 10:24
  • I think you can find a way [here in the JavaDocs of `SimpleFormat`](https://docs.oracle.com/javase/7/docs/api/java/util/logging/SimpleFormatter.html). – deHaar Jul 23 '19 at 10:51
  • Possible duplicate of [How do i modify a log format with Simple Formatter?](https://stackoverflow.com/questions/49186029/how-do-i-modify-a-log-format-with-simple-formatter) – jmehrens Jul 25 '19 at 20:55

2 Answers2

0

This entirely depends on the logging framework you are using. For example:

  • If you are just using System.out.println() it is as simple as changing the string you print

  • If you are using Logback, you can define custom layout classes that extend LayoutBase<ILoggingEvent>, and set it in your layouts xml file.

In general, look up the documentation for the logging framework you are using, or provide this information in the question.

cameron1024
  • 9,083
  • 2
  • 16
  • 36
0

The best way to solve the problem is create a class that extend Formatter and create your own format. This worked for me:

private static final DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

public String format(LogRecord record) {
    StringBuilder builder = new StringBuilder(1000);
    builder.append(df.format(new Date(record.getMillis()))).append(" - ");
    builder.append("[").append(record.getLevel()).append("] : ");
    builder.append(formatMessage(record));
    builder.append("\n");
    return builder.toString();
}

public String getHead(Handler h) {
    return super.getHead(h);
}

public String getTail(Handler h) {
    return super.getTail(h);
}