Specify time zone in log outputs

1

I am developing a project in Kotlin and Spring and a question has arisen with a log and the time zone used. We use LoggerFactory to write to the standard output information about the events (an event log) but the The time it shows is not adequate.

This is the code (reduced and pseudo-encoded in some parts) of my main class:

package com.micompania.midepartamento.miproyecto

import org.slf4j.LoggerFactory
// otros imports

@SpringBootApplication
// otras anotaciones
open class miApplication {
  companion object {
    // algunas cosas por aquí
    @Throws(IOException::class)
    @JvmStatic
    fun main(args: Array<String>) {
      val log = LoggerFactory.getLogger(miApplication::class.java)
      log.info("Arrancando ${miApplication::class.simpleName} ...")
      // más cosas por acá
      SpringApplication.run(miApplication::class.java, *args)
    }
  }
}

This works without problems, and in the standard output I see something like:

  

2018-01-31 11: 22: 32.542 [main] INFO com.micompania.midepartamento.miproyecto.miApplication - Starting MiApplication ...

Which is correct. But there is a problem: the time that appears is the local time of the machine in which the project runs, and to avoid confusion we want all hours to be in UTC. Is there any way to tell the logger to use the UTC time zone? What would it be like?

    
asked by Alvaro Montoro 31.01.2018 в 21:40
source

1 answer

0

The solution I used at the end was to change the application's time zone (and not just the logger's) so that the dates and times were in UTC. This can be done like this:

TimeZone.setDefault(TimeZone.getTimeZone("UTC"))

As a precaution, verify that the time zone is not modified in any other part of the project. And in the end, the code of the main class was like this:

package com.micompania.midepartamento.miproyecto

import org.slf4j.LoggerFactory
import java.util.TimeZone
// otros imports

@SpringBootApplication
// otras anotaciones
open class miApplication {
  companion object {
    // algunas cosas por aquí
    @Throws(IOException::class)
    @JvmStatic
    fun main(args: Array<String>) {
      // especificar zona horaria UTC para el log
      TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
      val log = LoggerFactory.getLogger(miApplication::class.java)
      log.info("Arrancando ${miApplication::class.simpleName} ...")
      // más cosas por acá
      SpringApplication.run(miApplication::class.java, *args)
    }
  }
}
    
answered by 01.02.2018 / 17:38
source