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 ''NbFormatter'' output. Note, using ''NbFormatter'' requires a private package reference to the ''org-netbeans-core-startup'' module. This will FAQ show how to establish private package access.

+

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 ''NbFormatter'' output. 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====

+

==== 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 module dependency to the POM:

+

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

<source lang="xml">

<source lang="xml">

-

<dependency>

+

<dependency>

-

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

+

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

-

<artifactId>org-netbeans-core-startup</artifactId>

+

<artifactId>org-netbeans-core-startup</artifactId>

-

<groupId>org.netbeans.modules</groupId>

+

<groupId>org.netbeans.modules</groupId>

-

<version>${netbeans.version}</version>

+

<version>${netbeans.version}</version>

-

</dependency>

+

</dependency>

</source>

</source>

-

==== Step #2. Create the Custom Log Formatter====

+

==== Step 2. Create the Custom Log Formatter====

-

Here's an example of a custom ''Formatter''. It uses ''NbFormatter'' to obtain a formatted log message from the NetBeans logging mechanism, and prepends a timestamp to it.

+

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 the timestamp, extracted from the LogRecord, to the formatted log message.

<source lang="java">

<source lang="java">

Line 65:

Line 62:

}

}

}

}

-

</source>

</source>

Line 74:

Line 70:

<source lang="java">

<source lang="java">

-

+

LogFormatter formatter = new LogFormatter(); // Custom formatter

-

LogFormatter formatter = new LogFormatter();

+

// Override the default formatters with the custom formatter

-

+

Logger logger = Logger.getLogger (""); // Root logger

-

Logger logger = Logger.getLogger (""); // root logger

+

Handler[] handlers = logger.getHandlers();

-

Handler[] handlers = logger.getHandlers();

+

for (Handler handler : handlers) {

-

for (int i = 0; i < handlers.length; i++) {

+

handler.setFormatter(formatter);

-

// Override the default formatter

+

}

-

handlers[i].setFormatter(formatter);

+

-

}

+

-

+

</source>

</source>

Line 89:

Line 82:

====Step 4. Configure Access to Private Package====

====Step 4. Configure Access to Private Package====

-

In the module POM, configure the ''nbm-maven-plugin'' dependencies to allow the private package access:

+

In the module POM, configure the '''nbm-maven-plugin''' dependencies to allow the private package access to '''org.netbeans.modules:org-netbeans-core-startup''' via a ''''impl'''' module dependency:

Please note, 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 disabled, and either the default Java logging or your custom logging class is used instead.

Solution

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 NbFormatter output. 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 dependency to the project POM:

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 the timestamp, extracted from the LogRecord, to the formatted log message.