How to Get Java Logging Output to Appear on a Single Line

How to continue log on same line in Java?

Without knowing precisely which logging library you are using, I would guess that the answer is almost certainly not unless you want to explicitly specify a newline character everywhere else.

The java.util.logging (and Log4J) frameworks allow you to plugin formatters to format your statements. You'd need to specify a format which did not end with a newline character. For example, in Log4J. they give an example pattern as

 %-5p [%t]: %m%n

So if you removed everything except the %m (the %n is the newline and the other stuff includes the time and log-level; %t and %p respectively, I think), newlines would not be automatically appended to each statement.

However, this means that everywhere else, your other log statements would have to look like this:

log.info("I want this on one line\n");

How do I suppress the date line from 2-line java.util.logging output?

You need to create a a different Formatter and use it instead.

public class BriefFormatter extends Formatter 
{
public BriefFormatter() { super(); }

@Override
public String format(final LogRecord record)
{
return record.getMessage();
}
}

combine information from multi line log to single line

gawk solution:

awk '!f && /ContainerRequestContextFilter/{ f=1; t=$1 FS $2; next }
/Client IP Address/{ ip=$NF; next }
/Client User-Agent/{ uagent=$10; for(i=11;i<=NF;i++) uagent=uagent FS $i; next }
/userID =.* token =/{ match($0,/userID = ([0-9]+), token = (.+)/,a); f=0;
printf("%s, %s, %s, %s, %s\n",t,ip,uagent,a[1],a[2]); next}
' system.log

The output:

2017-05-31 16:30:38.889, 172.25.4.32, Dalvik/2.1.0 (Linux; U; Android 7.1.2; Nexus 5X Build/N2G47O), 123456789, AppleBallCatDogElephantFoxGoatHorse
2017-05-31 16:42:36.500, 172.25.4.32, MyiOSApp/v2.2.2 (build:888; iOS 10.3.2) Alamofire/4.4.0, 987654321, KingLionMonkeyNestOwlParrotQueenRabbitSnakeTiger

  • !f && /ContainerRequestContextFilter/ - processing the starting ContainerRequestContextFilter line

  • /Client IP Address/{ ip=$NF; next } - processing line containing Client IP Address value

  • /Client User-Agent/ - processing line matching Client User-Agent

  • /userID =.* token =/ - processing line matching userID = and token =

Get line number in log handler

There is no LogRecord (JDK1.4 - JDK1.7) method to get the line number. If no thread handoff is occurring in your handler or upstream you can create a new Throwable().getStackTrace() and use the java.lang.StackTraceElement API to get the line number.

Example code can be found in the JavaMail API at MailLogger.inferCaller()/isLoggerImplFrame(). If you have to deal with thread handoffs, then you would have to log the line number as a log record parameter. Keep in mind that computing the callsite is expensive in terms of performance.

java.util.logging: how to suppress date line

The problem is caused by a handler in the parent log. The solution is to remove all handlers from the parent log, and then add own custom handler. This code removes handlers from the parent log:


for(Handler iHandler:log.getParent().getHandlers())
{
log.getParent().removeHandler(iHandler);
}

How to output long line with FileHandler?

The FileHandler has a limit as described in its documentation:

<handler-name>.limit specifies an approximate maximum amount to write (in bytes) to any one file. If this is zero, then there is no limit. (Defaults to no limit).

It states the default as being no limit, but the limit is set by the logging.properties file found in the used JRE/JDK installation directory. On my system, Java 8 or Java 17, this file contains the line

java.util.logging.FileHandler.limit = 50000

setting the limit to 50000.

Possible solutions:

  • set the limit in the constructor of FileHandler, e.g:
new FileHandler("%h/java%u.log", 0, 1);  // using the default file pattern
  • having a different configuration file (logging.properties) with adjusted limit, specified by the java.util.logging.config.file system property.


Related Topics



Leave a reply



Submit