func SetLogFile(loger *log.Logger, logPath string, log_type LOG_TYPE) error { var logFile *os.File var err error var checkFile = func() error { logPathCur := logPath + "." + time.Now().Format(string(log_type)) if !fs.FileExists(logPathCur) { if logFile != nil { logFile.Close() logFile = nil } fs.DirCheck(logPathCur) } if logFile == nil { logFile, err = os.OpenFile(logPathCur, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644) if err != nil { log.Println("create log file failed [", logPathCur, "]", err) } loger.SetOutput(logFile) } return err } checkFile() time_util.SetInterval(func() { checkFile() }, 1) return err }
func logToFile(logger *log.Logger, filename string) { f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { logger.Fatalf("error opening file: %v", err) } logger.SetOutput(f) }
// CopyLoggerTo arranges for messages to be written to any log.Logger, see // CopyStandardLogTo for details on behaviour and details func CopyLoggerTo(name string, logger *stdLog.Logger) { sev, ok := severityByName(name) if !ok { panic(fmt.Sprintf("log.CopyLoggerTo(%q): unrecognized severity name", name)) } // Set a log format that captures the user's file and line: // d.go:23: message logger.SetFlags(stdLog.Lshortfile) logger.SetOutput(logBridge(sev)) }
// openFile is for output log file func openFile(logger *log.Logger, fileName string) { if fileName == "" { return } f, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Fatal("Error opening file :", err.Error()) } logger.SetOutput(f) }
func writeAndSwap(log *log.Logger, msg string, stderr bool) { log.SetOutput(g_rlog) log.Printf(msg) if g_silent { log.SetOutput(ioutil.Discard) } else if stderr { log.SetOutput(os.Stderr) } else { log.SetOutput(os.Stdout) } }