How Do I Add a Timestamp to the Logs?

This FAQ entry demonstrates how to prepend a timestamp to your log file entries. For example:

05:42:51.210 CONFIG [com.emxsys.wmt.core.project.GlobalActionContextProxy]: Creating a proxy ....
05:42:51.295 WARNING [org.openide.filesystems.Ordering]: Not all children in ....
05:42:52.151 INFO [org.netbeans.core.startup.NbEvents]: Turning on modules: ....


The NetBeans Platform includes its own logging mechanism. If you customize the logging through the use of the java.util.logging.config.file or java.util.logging.config.class property settings, then the native NetBeans logging mechanism is completely disabled, and either the default Java logging or your custom logging class is used instead.


This solution shows how to customize the output from the native NetBeans log formatter (NbFormatter) by creating a new custom formatter that by prepends a timestamp to the log messages. Note, using NbFormatter requires a private package reference to the org-netbeans-core-startup module. This FAQ will also show how to establish the private package access.

Step 1. Establish Project Dependencies

Include the a dependency on org-netbeans-core-startup in the project that will implement the custom formatter. In Maven, add following entry to the project POM:

    <!--Private Package References: see maven plugin dependencies ...-->

Step 2. Create the Custom Log Formatter

Here's an example of a custom Formatter. It uses the NetBeans NbFormatter instance to obtain a formatted message from the LogRecord. NbFormatter is a final class that exposes itself via a public static FORMATTER property. This solution simply prepends a timestamp, extracted from the LogRecord, to the formatted log message.

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import org.netbeans.core.startup.logging.NbFormatter; // Private Package Referenced: See POM notes.
public class LogFormatter extends Formatter {
    public String format(LogRecord record) {
        String logMsg = NbFormatter.FORMATTER.format(record);
        StringBuilder sb = new StringBuilder();
        // Prepend a timestamp
        Instant instant = Instant.ofEpochMilli(record.getMillis());
        ZonedDateTime timestamp = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
        sb.append(' ');
        return sb.toString();

Step 3. Override the Default Formatters

In a module Installer class, include the following code block in the restored method to replace the default formatters with the custom formatter:

public class Installer extends ModuleInstall {
    public void restored() {
        // Override the default formatters with the custom formatter
        LogFormatter formatter = new LogFormatter(); // Custom formatter
        Logger logger = Logger.getLogger ("");       // Root logger
        Handler[] handlers = logger.getHandlers();
        for (Handler handler : handlers) {

Step 4. Configure Access to Private Package

In the project's POM, edit the nbm-maven-plugin configuration to allow private package access to org.netbeans.modules:org-netbeans-core-startup via an impl module dependency, for example:

                <!--Private Package Reference-->

Final Notes

To leverage the NetBeans logging, you must disable any java.util.logging.config.file or java.util.logging.config.class property settings. Check your application's .conf file, and, in the application's POM, check the additionalArguments entry.

This example was tested with NetBeans 8.0 and JDK 8.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo