Java : Formatter (logging) with Examples
Formatter (Java SE 17 & JDK 17) API Examples.
You will find code examples on most Formatter methods.
Summary
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
final var handler = new ConsoleHandler();
logger.addHandler(handler);
// SimpleFormatter
System.out.println(handler.getFormatter()); // java.util.logging.SimpleFormatter@d76b01a
logger.info("TEST LOG!");
// Result
// ↓
//Mar 17, 2022 1:04:31 AM com.example.logging.FormatterTest$1 method
//INFO: TEST LOG!
// XMLFormatter
handler.setFormatter(new XMLFormatter());
logger.info("TEST LOG!");
// Result
// ↓
//<record>
// <date>2022-03-17T08:04:31.426182700Z</date>
// <millis>1647504271426</millis>
// <nanos>182700</nanos>
// <sequence>2</sequence>
// <logger>com.example.logging</logger>
// <level>INFO</level>
// <class>com.example.logging.FormatterTest$1</class>
// <method>method</method>
// <thread>1</thread>
// <message>TEST LOG!</message>
//</record>
// A customized formatter.
class SingleLineFormatter extends Formatter {
@Override
public String format(LogRecord logRecord) {
final var message = formatMessage(logRecord);
return "%s (%s) : %s%n".formatted(
logRecord.getInstant(), logRecord.getLevel().getName(), message);
}
}
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
final var handler = new ConsoleHandler();
logger.addHandler(handler);
handler.setFormatter(new SingleLineFormatter());
logger.info("TEST LOG : 1");
logger.info("TEST LOG : 2");
// Result
// ↓
//2022-03-17T05:10:41.581937800Z (INFO) : TEST LOG : 1
//2022-03-17T05:10:41.584935200Z (INFO) : TEST LOG : 2
Constructors
Formatter ()
Please see format(LogRecord record).
Methods
abstract String format (LogRecord record)
class MyFormatter extends Formatter {
@Override
public String format(LogRecord logRecord) {
final var message = formatMessage(logRecord);
return "%s: %s%n".formatted(logRecord.getLevel().getName(), message);
}
}
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
final var handler = new ConsoleHandler();
logger.addHandler(handler);
handler.setFormatter(new MyFormatter());
logger.info("TEST LOG : 1");
logger.info("TEST LOG : 2");
// Result
// ↓
//INFO: TEST LOG : 1
//INFO: TEST LOG : 2
String formatMessage (LogRecord record)
package com.example.logging;
import java.util.ListResourceBundle;
public class LoggerEnResourceBundle extends ListResourceBundle {
@Override
protected Object[][] getContents() {
return new Object[][]{
{"SAMPLE", "XXX"}
};
}
}
class MyFormatter extends Formatter {
@Override
public String format(LogRecord logRecord) {
final var message = formatMessage(logRecord);
return "%s: %s%n".formatted(logRecord.getLevel().getName(), message);
}
}
final var logger = Logger.getLogger(
"com.example.logging",
"com.example.logging.LoggerEnResourceBundle");
logger.setUseParentHandlers(false);
final var handler = new ConsoleHandler();
logger.addHandler(handler);
handler.setFormatter(new MyFormatter());
logger.info("SAMPLE");
// Result
// ↓
//INFO: XXX
String getHead (Handler h)
class MyFormatter extends Formatter {
@Override
public String format(LogRecord logRecord) {
final var message = formatMessage(logRecord);
return "%s (%s) : %s%n".formatted(
logRecord.getInstant(), logRecord.getLevel().getName(), message);
}
@Override
public String getHead(Handler h) {
return "--- head ---%n".formatted();
}
@Override
public String getTail(Handler h) {
return "--- tail ---%n".formatted();
}
}
final var logger = Logger.getLogger("com.example.logging");
logger.setUseParentHandlers(false);
final var pattern = Path.of("R:", "java-work", "log.txt");
final var handler = new FileHandler(pattern.toString());
logger.addHandler(handler);
handler.setFormatter(new MyFormatter());
logger.info("TEST LOG : 1");
logger.info("TEST LOG : 2");
logger.removeHandler(handler);
handler.close();
System.out.print(Files.readString(pattern));
// Result
// ↓
//--- head ---
//2022-03-17T05:18:12.308480200Z (INFO) : TEST LOG : 1
//2022-03-17T05:18:12.310478600Z (INFO) : TEST LOG : 2
//--- tail ---
String getTail (Handler h)
Please see getHead(Handler h).
Related posts
- API Examples