在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
原文:https://www.goinggo.net/2013/11/using-log-package-in-go.html ---------------------------------------------------------------------------------------------------------------- Linux is unique to Windows in many ways, and writing programs in Linux is no exception. The use of standard out, standard err and null devices is not only a good idea but it’s the law. If your programs are going to be logging information, it is best to follow the destination conventions. This way your programs will work with all of the Mac/Linux tooling and hosted environments. package main
import ( "io" "io/ioutil" "log" "os" ) var ( Trace *log.Logger Info *log.Logger Warning *log.Logger Error *log.Logger ) func Init( traceHandle io.Writer, infoHandle io.Writer, warningHandle io.Writer, errorHandle io.Writer) { Trace = log.New(traceHandle, "TRACE: ", log.Ldate|log.Ltime|log.Lshortfile) Info = log.New(infoHandle, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) Warning = log.New(warningHandle, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile) Error = log.New(errorHandle, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) } func main() { Init(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr) Trace.Println("I have something standard to say") Info.Println("Special Information") Warning.Println("There is something you need to know about") Error.Println("Something has failed") }
INFO: 2013/11/05 18:11:01 main.go:44: Special Information
WARNING: 2013/11/05 18:11:01 main.go:45: There is something you need to know about ERROR: 2013/11/05 18:11:01 main.go:46: Something has failed
var Trace *log.Logger
Trace = log.New(traceHandle, "TRACE: ", log.Ldate|log.Ltime|log.Lshortfile) Init(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr) Trace.Println("I have something standard to say")
func New(out io.Writer, prefix string, flag int) *Logger
out: The out variable sets the destination to which log data will be written. prefix: The prefix appears at the beginning of each generated log line. flags: The flag argument defines the logging properties. Flags: const ( // Bits or’ed together to control what’s printed. There is no control over the // order they appear (the order listed here) or the format they present (as // described in the comments). A colon appears after these items: // 2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message Ldate = 1 << iota // the date: 2009/01/23 Ltime // the time: 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 LstdFlags = Ldate | Ltime // initial values for the standard logger )
var Info *log.Logger
Info = log.New(infoHandle, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) Init(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr) Info.Println("Special Information")
var Error *log.Logger
Error = log.New(errorHandle, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) Init(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr) Error.Println("Special Information")
file, err := os.OpenFile("file.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil { log.Fatalln("Failed to open log file", output, ":", err) } MyFile = log.New(file, "PREFIX: ", log.Ldate|log.Ltime|log.Lshortfile)
file, err := os.OpenFile("file.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil { log.Fatalln("Failed to open log file", output, ":", err) } multi := io.MultiWriter(file, os.Stdout) MyFile := log.New(multi, "PREFIX: ", log.Ldate|log.Ltime|log.Lshortfile)
package main
import ( "log" ) func main() { log.Println("Hello World") }
2013/11/05 18:42:26 Hello World
package main
import ( "log" ) func main() { log.SetFlags(0) log.Println("Hello World") }
Hello World
package main
import ( "io/ioutil" "log" ) func main() { log.SetOutput(ioutil.Discard) log.Println("Hello World") }
|
请发表评论