3

I am using java logging in Groovy and I wanted to modify the format string so I only have one line instead of two, but the methods I tried didn't work - I looked at this question:

How do I get java logging output to appear on a single line?

I tried passing the property to groovy, but it didn't change the format.

I passed it like this:

groovy myScript.groovy -Djava.util.logging.SimpleFormatter.format=%1$tF %1$tT

but it doesn't look like it was picked up.

Community
  • 1
  • 1
Roman Goyenko
  • 6,965
  • 5
  • 48
  • 81

2 Answers2

3

Here is what I did - I added this code to my groovy:

    System.setProperty("java.util.logging.SimpleFormatter.format",
 '%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %5$s%6$s%n');
    Logger log = Logger.getLogger("")
    FileHandler fh = new FileHandler("../log/replication.log")
    log.addHandler(fh)
    SimpleFormatter formatter = new SimpleFormatter()
    fh.setFormatter(formatter)

which didn't require me to modify the java properties in command line (I didn't want to create additional script to start my groovy script).

Roman Goyenko
  • 6,965
  • 5
  • 48
  • 81
2

You can use JAVA_OPTS for that. For example,

import java.util.logging.*

Logger log = Logger.getLogger('test')
log.setLevel(Level.INFO)

log.info('Test info log')
log.warning('Test warning')
log.config('Test config')
log.fine('Test fine')

and setting (i.e. below for windows):

set JAVA_OPTS="-Djava.util.logging.SimpleFormatter.format=%1$tF %1$tT [%4$s] %5$s %n"

Running the above sample script yields:

> groovy testLoggingJavaUtil.groovy
2015-03-27 17:35:48 [INFO] Test info log
2015-03-27 17:35:48 [WARNING] Test warning
jalopaba
  • 8,039
  • 2
  • 44
  • 57