DevFaqAddTimestampToLogs

(Difference between revisions)
Line 9: Line 9:
<pre>
<pre>
-
05:42:51.210 CONFIG [com.emxsys.wmt.core.project.GlobalActionContextProxy]: Creating a proxy for Utilities.actionsGlobalContext()
+
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 Services/MIMEResolver/ marked with the position attribute: ....
+
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:
+
05:42:52.151 INFO [org.netbeans.core.startup.NbEvents]: Turning on modules: ....
-
05:43:08.030 INFO [com.terramenta.globe.WorldWindManager]: Session has been restored.
+
-
05:43:12.823 INFO [org.netbeans.core.ui.warmup.DiagnosticTask]: Total memory 4,168,376,320
+
-
05:43:08.040 CONFIG [com.emxsys.wmt.core.project.MainWindowTitleManager]: Initializing global context lookup listener for Projects
+
</pre>
</pre>
Line 21: Line 18:
===Solution===
===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 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:
<source lang="xml">
<source lang="xml">
-
            <plugin>
+
<plugin>
-
                <artifactId>nbm-maven-plugin</artifactId>
+
    <artifactId>nbm-maven-plugin</artifactId>
-
                <groupId>org.codehaus.mojo</groupId>
+
    <groupId>org.codehaus.mojo</groupId>
-
                <extensions>true</extensions>
+
    <extensions>true</extensions>
-
                <configuration>
+
    <configuration>
-
                    <moduleDependencies>
+
        <moduleDependencies>
-
                        <dependency>
+
            <dependency>
-
                            <!--Private Package Reference-->
+
                <!--Private Package Reference-->
-
                            <id>org.netbeans.modules:org-netbeans-core-startup</id>
+
                <id>org.netbeans.modules:org-netbeans-core-startup</id>
-
                            <type>impl</type>
+
                <type>impl</type>
-
                        </dependency>
+
            </dependency>
-
                    </moduleDependencies>
+
        </moduleDependencies>
-
                </configuration>
+
    </configuration>
-
            </plugin>
+
</plugin>
-
 
+
</source>
</source>
Line 112: Line 104:
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.
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.

Revision as of 17:24, 20 June 2014

DevFaqAddTimestampToLogs

Friday, June 20, 2014 5:34 AM

Contents

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: ....

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:

<dependency>
    <!--Private Package References: see maven plugin dependencies ...-->
    <artifactId>org-netbeans-core-startup</artifactId>
    <groupId>org.netbeans.modules</groupId>
    <version>${netbeans.version}</version>
</dependency>


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.

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 {
 
    @Override
    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(timestamp.toLocalTime.toString());
        sb.append(' ');
        sb.append(logMsg);
 
        return sb.toString();
    }
}

Step 3. Override the Default Formatters

In a module Installer class, include the following code block:


LogFormatter formatter = new LogFormatter(); // Custom formatter
// Override the default formatters with the custom formatter
Logger logger = Logger.getLogger ("");       // Root logger
Handler[] handlers = logger.getHandlers();
for (Handler handler : handlers) {
    handler.setFormatter(formatter);
}


Step 4. Configure Access to Private Package

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:

<plugin>
    <artifactId>nbm-maven-plugin</artifactId>
    <groupId>org.codehaus.mojo</groupId>
    <extensions>true</extensions>
    <configuration>
        <moduleDependencies>
            <dependency>
                <!--Private Package Reference-->
                <id>org.netbeans.modules:org-netbeans-core-startup</id>
                <type>impl</type>
            </dependency>
        </moduleDependencies>
    </configuration>
</plugin>

Final Notes

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