|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/Sirupsen/logrus" |
| 6 | + "github.com/fatih/color" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + typeRequest = 1 << iota |
| 15 | + typeRequestResponse = 1 << iota |
| 16 | + typeRequestResponseError = 1 << iota |
| 17 | + typeEvent = 1 << iota |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + levelConnection = 1 << iota |
| 22 | + levelProtocol = 1 << iota |
| 23 | + levelTarget = 1 << iota |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + fieldLevel = "level" |
| 28 | + fieldType = "type" |
| 29 | + fieldTargetId = "targetId" |
| 30 | + fieldRequest = "request" |
| 31 | + fieldMethod = "method" |
| 32 | + fieldInspectorId = "inspectorId" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + requestReplyFormat = "%-17s %s % 48s(%s) = %s\n" |
| 37 | + requestFormat = "%-17s %s % 48s(%s)\n" |
| 38 | + eventFormat = "%-17s %s % 48s(%s)\n" |
| 39 | + protocolFormat = "%-17s %s\n" |
| 40 | + timeFormat = "15:04:05.00000000" |
| 41 | +) |
| 42 | + |
| 43 | +const logsDir = "logs/%s.log" |
| 44 | + |
| 45 | +var ( |
| 46 | + responseColor = color.New(color.FgHiGreen).SprintfFunc() |
| 47 | + requestColor = color.New(color.FgHiBlue).SprintFunc() |
| 48 | + requestReplyColor = color.New(color.FgGreen).SprintfFunc() |
| 49 | + eventsColor = color.New(color.FgHiRed).SprintfFunc() |
| 50 | + protocolColor = color.New(color.FgYellow).SprintfFunc() |
| 51 | + protocolError = color.New(color.FgHiYellow, color.BgRed).SprintfFunc() |
| 52 | + targetColor = color.New(color.FgHiWhite).SprintfFunc() |
| 53 | + methodColor = color.New(color.FgHiYellow).SprintfFunc() |
| 54 | + errorColor = color.New(color.BgRed, color.FgWhite).SprintfFunc() |
| 55 | +) |
| 56 | + |
| 57 | +type FramesFormatter struct { |
| 58 | +} |
| 59 | + |
| 60 | +func (f FramesFormatter) Format(e *logrus.Entry) ([]byte, error) { |
| 61 | + message := e.Message |
| 62 | + timestamp := e.Time.Format(timeFormat) |
| 63 | + |
| 64 | + var protocolType int = -1 |
| 65 | + var protocolMethod string = "" |
| 66 | + |
| 67 | + protocolLevel := e.Data[fieldLevel].(int) |
| 68 | + |
| 69 | + if val, ok := e.Data[fieldType].(int); ok { |
| 70 | + protocolType = val |
| 71 | + } |
| 72 | + |
| 73 | + if val, ok := e.Data[fieldMethod].(string); ok { |
| 74 | + protocolMethod = val |
| 75 | + } |
| 76 | + |
| 77 | + switch protocolLevel { |
| 78 | + case levelConnection: |
| 79 | + switch e.Level { |
| 80 | + case logrus.ErrorLevel: |
| 81 | + return []byte(fmt.Sprintf(protocolFormat, timestamp, errorColor(message))), nil |
| 82 | + case logrus.InfoLevel: |
| 83 | + return []byte(fmt.Sprintf(protocolFormat, timestamp, protocolColor(message))), nil |
| 84 | + } |
| 85 | + |
| 86 | + case levelProtocol, levelTarget: |
| 87 | + targetId := e.Data[fieldTargetId].(string) |
| 88 | + |
| 89 | + switch protocolType { |
| 90 | + case typeEvent: |
| 91 | + return []byte(fmt.Sprintf(eventFormat, timestamp, targetColor(targetId), methodColor(protocolMethod), eventsColor(message))), nil |
| 92 | + |
| 93 | + case typeRequest: |
| 94 | + return []byte(fmt.Sprintf(requestFormat, timestamp, targetColor(targetId), methodColor(protocolMethod), requestColor(message))), nil |
| 95 | + |
| 96 | + case typeRequestResponse: |
| 97 | + return []byte(fmt.Sprintf(requestReplyFormat, timestamp, targetColor(targetId), methodColor(protocolMethod), requestReplyColor(e.Data[fieldRequest].(string)), responseColor(message))), nil |
| 98 | + |
| 99 | + case typeRequestResponseError: |
| 100 | + return []byte(fmt.Sprintf(requestReplyFormat, timestamp, targetColor(targetId), methodColor(protocolMethod), requestReplyColor(e.Data[fieldRequest].(string)), errorColor(message))), nil |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return []byte(fmt.Sprintf("unsupported entry: %+v", e)), nil |
| 105 | +} |
| 106 | + |
| 107 | +func createLogWriter(filename string) (io.Writer, error) { |
| 108 | + |
| 109 | + if filename == "" { |
| 110 | + if *flagQuiet { |
| 111 | + return ioutil.Discard, nil |
| 112 | + } |
| 113 | + |
| 114 | + return os.Stdout, nil |
| 115 | + } |
| 116 | + |
| 117 | + logFilePath := fmt.Sprintf(logsDir, filename) |
| 118 | + |
| 119 | + dir := filepath.Dir(logFilePath) |
| 120 | + |
| 121 | + if _, err := os.Stat(dir); err != nil { |
| 122 | + if err := os.MkdirAll(dir, 0777); err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + writer, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0777) |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + |
| 132 | + if *flagQuiet { |
| 133 | + return writer, nil |
| 134 | + } |
| 135 | + |
| 136 | + return io.MultiWriter(writer, os.Stdout), nil |
| 137 | +} |
| 138 | + |
| 139 | +func createLogger(filename string) (*logrus.Logger, error) { |
| 140 | + |
| 141 | + writer, err := createLogWriter(filename) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + |
| 146 | + return &logrus.Logger{ |
| 147 | + Out: writer, |
| 148 | + Formatter: new(FramesFormatter), |
| 149 | + Hooks: make(logrus.LevelHooks), |
| 150 | + Level: logrus.DebugLevel, |
| 151 | + }, nil |
| 152 | +} |
0 commit comments