如何更改Go的日志包的日期/时间格式

huangapple go评论84阅读模式
英文:

How to change the date/time format of Go's log package

问题

使用log包时,Go语言会输出类似以下格式的内容:

2009/11/10 23:00:00 Hello, world

我该如何将日期和时间格式更改为类似于 dd.mm.yyy hh:mm:ss 的格式?以下是一个示例(playground链接):

package main

import "log"

func main() {
    log.Println("Hello, playground")
}
英文:

When using the log package, Go outputs something like

2009/11/10 23:00:00 Hello, world

How can I change the date and time format to something like dd.mm.yyy hh:mm:ss? Example (playground link):

package main

import "log"

func main() {
	log.Println("Hello, playground")
}

答案1

得分: 63

像yed posterior所说的那样,你可以通过实现一个write函数来定义一个自定义的io.Writer。你可能还想执行log.SetFlags(0)来完全控制日志输出。下面是一个示例,它改变了日期格式,并添加了一些日志级别信息。

type logWriter struct {
}

func (writer logWriter) Write(bytes []byte) (int, error) {
    return fmt.Print(time.Now().UTC().Format("2006-01-02T15:04:05.999Z") + " [DEBUG] " + string(bytes))
}

func main() {

    log.SetFlags(0)
    log.SetOutput(new(logWriter))
    log.Println("This is something being logged!")
}

输出结果为:

2016-03-21T19:54:28.563Z [DEBUG] This is something being logged!
英文:

Like yed posterior said, you can define a custom io.Writer by implementing a write function. You'll also probably want to do a log.SetFlags(0) to take full control. Here's an example that changes the date format as well as adds some log level info.

type logWriter struct {
}

func (writer logWriter) Write(bytes []byte) (int, error) {
	return fmt.Print(time.Now().UTC().Format("2006-01-02T15:04:05.999Z") + " [DEBUG] " + string(bytes))
}

func main() {

    log.SetFlags(0)
	log.SetOutput(new(logWriter))
    log.Println("This is something being logged!")
}

outputs:

2016-03-21T19:54:28.563Z [DEBUG] This is something being logged!

答案2

得分: 6

根据来源(http://golang.org/src/pkg/log/log.go),没有内置的方法来实现这一点:

26 // 用于控制打印的位或操作。无法控制它们出现的顺序(在此处列出的顺序)或它们的格式(如注释中所述)。这些项目后面会有一个冒号:
29 // 2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message

你需要使用第三方包来实现,或者像yed描述的那样拦截日志输出。

英文:

According to the source (http://golang.org/src/pkg/log/log.go) there is no built-in way to do that:

26		// Bits or'ed together to control what's printed. There is no control over the
27		// order they appear (the order listed here) or the format they present (as
28		// described in the comments).  A colon appears after these items:
29		//	2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message

You'll need to use a 3rd party package for that, or intercept the log output as yed described.

答案3

得分: 5

使用自定义的写入器来过滤日志行并将其修改为所需的格式。这应该很容易,因为标题的格式是规则的且固定宽度的。然后调用log.SetOutput(myFilterWriter(os.Stderr))。

英文:

Use a custom writer that filters the log lines to modify them to the format you need. It should be easy because the format of the header is regular and fixed-width. Then call log.SetOutput(myFilterWriter(os.Stderr)).

答案4

得分: 0

使用Logger系统内部的标志更容易。

log.SetFlags(log.Lmicroseconds)

使用此标志将时间戳添加到日志中,精确到微秒。
其他可用选项包括:

const (
Ldate         = 1 << iota     // 本地时区的日期:2009/01/23
Ltime                         // 本地时区的时间:01:23:23
Lmicroseconds                 // 微秒分辨率:01:23:23.123123。假设Ltime。
Llongfile                     // 完整的文件名和行号:/a/b/c/d.go:23
Lshortfile                    // 最终文件名元素和行号:d.go:23。覆盖Llongfile
LUTC                          // 如果设置了Ldate或Ltime,则使用UTC而不是本地时区
Lmsgprefix                    // 将“前缀”从行的开头移到消息之前
LstdFlags     = Ldate | Ltime // 标准记录器的初始值

)

Golang记录器文档可在此处找到。

英文:

It's much easier to use the flags from Logger system internal.

	log.SetFlags(log.Lmicroseconds)

Use this flag to add timestamp with microseconds to the log.
Other available options are :

const (
Ldate         = 1 &lt;&lt; iota     // the date in the local time zone: 2009/01/23
Ltime                         // the time in the local time zone: 01:23:23
Lmicroseconds                 // microsecond resolution: 01:23:23.123123.  assumes Ltime.
Llongfile                     // full file name and line number: /a/b/c/d.go:23
Lshortfile                    // final file name element and line number: d.go:23. overrides Llongfile
LUTC                          // if Ldate or Ltime is set, use UTC rather than the local time zone
Lmsgprefix                    // move the &quot;prefix&quot; from the beginning of the line to before the message
LstdFlags     = Ldate | Ltime // initial values for the standard logger

)

Golang logger documentation is available here

huangapple
  • 本文由 发表于 2014年9月30日 20:19:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/26120698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定