10 examples of 'golang log to file' in Go

Every line of 'golang log to file' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
11func Log(msg string) {
12 if LogFileMissing() {
13 return
14 }
15
16 f, err := os.OpenFile(LogFilePath(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
17 if err != nil {
18 log.Fatalf("error opening file: %v", err)
19 }
20 defer func() { _ = f.Close() }()
21
22 log.SetOutput(f)
23 log.Println(msg)
24}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

61func writeLog(i ...interface{}) {
62 if Debug {
63 log.Println("checkClient:", fmt.Sprint(i...))
64 }
65}
19func (s *Scheduler) log(f string, v ...interface{}) {
20 if s.verbose {
21 log.Printf(f, v...)
22 }
23}
191func (c *C) logf(format string, args ...interface{}) {
192 c.writeLog([]byte(fmt.Sprintf(format+"\n", args...)))
193}
92func (s *Server) Logf(f string, args ...interface{}) {
93 if s.api != nil && s.api.Logger != nil {
94 s.api.Logger(f, args...)
95 } else {
96 log.Printf(f, args...)
97 }
98}
75func log(v ...interface{}) {
76 if raceDetector {
77 globalMutex.Lock()
78 defer globalMutex.Unlock()
79 }
80 if globalLogger != nil {
81 globalLogger.Output(2, fmt.Sprint(v...))
82 }
83}
78func (node *nodeConn) logf(l logger.Level, format string, log ...interface{}) {
79 b := new(bytes.Buffer)
80 _, _ = fmt.Fprintf(b, format, log...)
81 _, _ = fmt.Fprint(b, "\n\n", node.conn)
82 node.ctx.logger.Print(l, "serve-node", b)
83}
19func saveLogs() {
20 logFile := log4go.NewFileLogWriter("stack.log", false)
21 logFile.SetFormat("%d %t - %M (%S)")
22 logFile.SetRotate(false)
23 logFile.SetRotateSize(0)
24 logFile.SetRotateLines(0)
25 logFile.SetRotateDaily(true)
26
27 logStack := make(log4go.Logger)
28 logStack.AddFilter("file", log4go.DEBUG, logFile)
29 for i := range LogItems {
30 fmt.Println(LogItems[i].Message)
31 logStack.Info(LogItems[i].Message)
32 }
33}
15func Console_Log(data []Object) {
16 var i []interface{}
17 for _, d := range data {
18 i = append(i, d)
19 }
20
21 fmt.Println(i...)
22}
331func (logger Logger) Logf(level Level, message string, args ...interface{}) {
332 if logger.getModule().getEffectiveLogLevel() > level ||
333 !WillWrite(level) ||
334 level < TRACE ||
335 level > CRITICAL {
336 return
337 }
338 // Gather time, and filename, line number.
339 now := time.Now() // get this early.
340 // Param to Caller is the call depth. Since this method is called from
341 // the Logger methods, we want the place that those were called from.
342 _, file, line, ok := runtime.Caller(2)
343 if !ok {
344 file = "???"
345 line = 0
346 }
347 // Trim newline off format string, following usual
348 // Go logging conventions.
349 if len(message) > 0 && message[len(message)-1] == '\n' {
350 message = message[0 : len(message)-1]
351 }
352
353 formattedMessage := fmt.Sprintf(message, args...)
354 writeToWriters(level, logger.impl.name, file, line, now, formattedMessage)
355}

Related snippets