Skip to content

help me set this formate:  #3

Closed

Description

@5nn

main.go -- func main() -- line 22-- 2018/01/07 18:49:01.1111 :This is a warning message

thank u ,

Activity

  1. kataras commented on Jan 8, 2018

    @kataras
    Owner

    @5nn Golog supports message hijacking, so you can output with your own format like @celsosz , a good example of this is the below (you can modify it to your specific needs):

    import (
        "runtime"
        "fmt"
    
        "github.com/kataras/golog"
    )
    // [...]
    golog.Handle(func(l *golog.Log) bool { /* or myLogger:= golog.New(...); myLogger.Handle or golog.Child(...).Handle */
    	prefix := golog.GetTextForLevel(l.Level, true)
    	pc, fn, line, _ := runtime.Caller(6)
    	message := fmt.Sprintf("%s line %d (%s) (%s) %s: %s",
    		prefix, line, runtime.FuncForPC(pc).Name(), fn, l.FormatTime(), l.Message)
    
    	if l.NewLine {
    		message += "\n"
    	}
    
    	fmt.Print(message)
    	return true
    })
    // [...]
    golog.Warnf("This is a warning message")
  2. virusdefender commented on May 29, 2019

    @virusdefender

    It's not a good idea to hard code the stack length pc, fn, line, _ := runtime.Caller(6), because the it is not fixed, for example

    package main
    import (
        "runtime"
        "fmt"
        "github.com/kataras/golog"
    )
    func main () {
    	golog.Handle(func(l *golog.Log) bool {
    		prefix := golog.GetTextForLevel(l.Level, true)
    		pc, fn, line, _ := runtime.Caller(7)
    		message := fmt.Sprintf("%s line %d (%s) (%s) %s: %s",
    			prefix, line, runtime.FuncForPC(pc).Name(), fn, l.FormatTime(), l.Message)
    
    		if l.NewLine {
    			message += "\n"
    		}
    
    		fmt.Print(message)
    		return true
    	})
    	golog.Warnf("This is a warning message")
    	golog.Warn("This is a warning message")
    }

    Output is

    [WARN] line 21 (main.main) (/tmp/main.go) 2019/05/29 08:18: This is a warning message
    [WARN] line 200 (runtime.main) (/usr/local/Cellar/go/1.12.1/libexec/src/runtime/proc.go) 2019/05/29 08:18: This is a warning message
    

    A better way is to manually check the stack frame

    package main
    import (
        "runtime"
        "fmt"
        "github.com/kataras/golog"
        "strings"
    )
    func main () {
    	golog.Handle(func(l *golog.Log) bool {
    		prefix := golog.GetTextForLevel(l.Level, true)
    		file := "???"
    		line := 0
    
    		pc := make([]uintptr, 64)
    		n := runtime.Callers(3, pc)
    		if n != 0 {
    			pc = pc[:n]
    			frames := runtime.CallersFrames(pc)
    
    			for {
    				frame, more := frames.Next()
    				if !strings.Contains(frame.File, "github.com/kataras/golog") {
    					file = frame.File
    					line = frame.Line
    					break
    				}
    				if !more {
    					break
    				}
    			}
    		}
    
    		slices := strings.Split(file, "/")
    		file = slices[len(slices)-1]
    
    		message := fmt.Sprintf("%s %s [%s:%d] %s",
    			prefix, l.FormatTime(), file, line, l.Message)
    
    		if l.NewLine {
    			message += "\n"
    		}
    
    		// fixme https://stackoverflow.com/a/14694666
    		fmt.Print(message)
    		return true
    	})
    	golog.Warnf("This is a warning message")
    	golog.Warn("This is a warning message")
    }

    Output is

    [WARN] 2019/05/29 08:19 [main.go:55] This is a warning message
    [WARN] 2019/05/29 08:19 [main.go:56] This is a warning message
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址