github.com/searKing/golang/go@v1.2.117/log/slog/glog_prerotate.go (about) 1 // Copyright 2022 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package slog 6 7 import ( 8 "bytes" 9 "fmt" 10 "os" 11 "path/filepath" 12 "runtime" 13 "strings" 14 "time" 15 16 os_ "github.com/searKing/golang/go/os" 17 ) 18 19 var ( 20 host = "unknownhost" 21 program = filepath.Base(os.Args[0]) 22 ) 23 24 func init() { 25 h, err := os.Hostname() 26 if err == nil { 27 host = shortHostname(h) 28 } 29 } 30 31 // shortHostname returns its argument, truncating at the first period. 32 // For instance, given "www.google.com" it returns "www". 33 func shortHostname(hostname string) string { 34 if i := strings.Index(hostname, "."); i >= 0 { 35 return hostname[:i] 36 } 37 return hostname 38 } 39 40 // GlogRotateHeader append rotate header to a file named by filename. 41 func GlogRotateHeader(name string) { 42 // Write header. 43 var buf bytes.Buffer 44 _, _ = fmt.Fprintf(&buf, "Log file created at: %s by %s\n", time.Now().Format("2006/01/02 15:04:05"), program) 45 _, _ = fmt.Fprintf(&buf, "Running on machine: %s\n", host) 46 _, _ = fmt.Fprintf(&buf, "Binary: Built with %s %s for %s/%s\n", runtime.Compiler, runtime.Version(), runtime.GOOS, runtime.GOARCH) 47 _, _ = fmt.Fprintf(&buf, "Log line format: [IWEF]yyyymmdd hh:mm:ss.uuuuuu threadid file:line(func)] msg\n") 48 _, _ = fmt.Fprintf(&buf, "Log line format: [IWEF] [yyyymmdd hh:mm:ss.uuuuuu] [threadid] [file:line(func)] msg\n") 49 _ = os_.AppendAll(name, buf.Bytes()) 50 }