Redirecting Platform Logs - Java Native OS

Java examples for Native OS:Log

Introduction

Utilize the Java 9 Unified Logging API to filter the logs and route accordingly.

java -Xlog:gc=debug:file=gc.txt:none Main

This will result in the creation of a file named gc.txt in the current directory, and all log messages pertaining to the specified -Xlog option will be written to it.

import java.time.LocalDate;
import java.time.LocalTime;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class Main {
    
    private static Logger log = LogManager.getLogger();
    public static void main(String[] args){
        log.debug("Time Plus 5 Hours: ");
        log.debug("Time Minus 30 Minutes: ");
        
    }
}

The following table lists the -Xlog Decorators.

Decorator Function
Time Provides current time and date in ISO-8601 format.
uptime Time that JVM has been up and running (in seconds and milliseconds).
timemillis Value returned from System.currentTimeInMillis();
uptimemillis Uptime of JVM in milliseconds.
timenanos Value returned from System.nanoTime();
uptimenanosUptime of JVM in nanoseconds.
pidProcess identifier.
tidThread identifier.
level Log message level.
tags Tag-set associated with log message.

Three types of output are supported, those being: stdout, stderr, and text file.


Related Tutorials