|
| 1 | +package logger |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + standardLog "log" |
| 7 | + "os" |
| 8 | + // "path" |
| 9 | + // "runtime" |
| 10 | + "strings" |
| 11 | + "sync" |
| 12 | + // "time" |
| 13 | + |
| 14 | + "github.com/sirupsen/logrus" |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +var _mutex *sync.RWMutex = nil |
| 19 | +var _indentLevel int = -1 |
| 20 | +var _indentation string = "" |
| 21 | +const INDENTATION_TOKEN = " " |
| 22 | +const INDENTATION_TOKEN_LENGTH = 4 |
| 23 | + |
| 24 | +var _logrusLogger *logrus.Logger |
| 25 | +var _logrusEntry *logrus.Entry |
| 26 | +var _logLevel logrus.Level |
| 27 | +var _strLogLevel string |
| 28 | + |
| 29 | + |
| 30 | +func init() { |
| 31 | + _mutex = &sync.RWMutex {} |
| 32 | + |
| 33 | + _indentLevel = 0 |
| 34 | + _indentation = "" |
| 35 | + |
| 36 | + _logrusLogger = logrus.New() |
| 37 | + _logrusLogger.AddHook(ContextHook{}) |
| 38 | + |
| 39 | + // |
| 40 | + // TODO: ...(maybe?) it would be nice it this could be made generic, |
| 41 | + // but this is getting set in init(). |
| 42 | + var logFilePath = os.Getenv("OCTOSCREEN_LOG_FILE_PATH") |
| 43 | + // |
| 44 | + |
| 45 | + if logFilePath == "" { |
| 46 | + standardLog.Print("logger.init() - logFilePath is was not defined. Now using just the standard console output.") |
| 47 | + _logrusLogger.Out = os.Stdout |
| 48 | + } else { |
| 49 | + standardLog.Printf("logger.init() - logFilePath is: %s", logFilePath) |
| 50 | + file, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) |
| 51 | + if err == nil { |
| 52 | + standardLog.Printf("logger.init() - OpenFile() succeeded and now setting log.Out to %s", logFilePath) |
| 53 | + _logrusLogger.Out = file |
| 54 | + |
| 55 | + _logrusLogger.Out = io.MultiWriter(os.Stdout, file) |
| 56 | + logrus.SetOutput(_logrusLogger.Out) |
| 57 | + } else { |
| 58 | + standardLog.Printf("logger.init() - OpenFile() FAILED! err is: %s", err.Error()) |
| 59 | + standardLog.Print("Failed to open the log file, defaulting to use the standard console output.") |
| 60 | + _logrusLogger.Out = os.Stdout |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + _logrusEntry = _logrusLogger.WithFields(logrus.Fields{}) |
| 65 | + |
| 66 | + // Start off with the logging level set to debug until we get a chance to read the configuration settings. |
| 67 | + SetLogLevel(logrus.DebugLevel) |
| 68 | +} |
| 69 | + |
| 70 | +func readIndentation() string { |
| 71 | + _mutex.RLock() |
| 72 | + var indentation = _indentation |
| 73 | + _mutex.RUnlock() |
| 74 | + |
| 75 | + return indentation |
| 76 | +} |
| 77 | + |
| 78 | +func writeIndentation(indentation string) { |
| 79 | + _mutex.Lock() |
| 80 | + _indentation = indentation |
| 81 | + _mutex.Unlock() |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +func SetLogLevel(newLevel logrus.Level) { |
| 86 | + _logLevel = newLevel |
| 87 | + _strLogLevel = strings.ToLower(_logLevel.String()) |
| 88 | + |
| 89 | + _logrusLogger.SetLevel(_logLevel) |
| 90 | + standardLog.Printf("logger.SetLogLevel() - the log level is now set to: %s", _strLogLevel) |
| 91 | +} |
| 92 | + |
| 93 | +func LogLevel() string { |
| 94 | + // Returns a lower case string. |
| 95 | + return _strLogLevel |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +func TraceEnter(functionName string) { |
| 100 | + message := fmt.Sprintf("%sentering %s", readIndentation(), functionName) |
| 101 | + _logrusEntry.Debug(message) |
| 102 | + _indentLevel++ |
| 103 | + indentation := readIndentation() + INDENTATION_TOKEN |
| 104 | + writeIndentation(indentation) |
| 105 | +} |
| 106 | + |
| 107 | +func TraceLeave(functionName string) { |
| 108 | + _indentLevel-- |
| 109 | + |
| 110 | + indentation := readIndentation() |
| 111 | + indentation = indentation[:(_indentLevel * INDENTATION_TOKEN_LENGTH)] |
| 112 | + writeIndentation(indentation) |
| 113 | + |
| 114 | + message := fmt.Sprintf("%sleaving %s", readIndentation(), functionName) |
| 115 | + _logrusEntry.Debug(message) |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +func LogError(currentFunctionName, functionCalledName string, err error) { |
| 120 | + if err != nil { |
| 121 | + _logrusEntry.Errorf("%s%s - %s returned an error: %q", readIndentation(), currentFunctionName, functionCalledName, err) |
| 122 | + } else { |
| 123 | + _logrusEntry.Errorf("%s%s - %s returned an error", readIndentation(), currentFunctionName, functionCalledName) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func LogFatalError(currentFunctionName, functionCalledName string, err error) { |
| 128 | + if err != nil { |
| 129 | + _logrusEntry.Fatalf("%s%s - %s returned an error: %q", readIndentation(), currentFunctionName, functionCalledName, err) |
| 130 | + } else { |
| 131 | + _logrusEntry.Fatalf("%s%s - %s returned an error", readIndentation(), currentFunctionName, functionCalledName) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +func Debug(args ...interface{}) { |
| 137 | + _logrusEntry.Debug(readIndentation() + fmt.Sprint(args...)) |
| 138 | +} |
| 139 | + |
| 140 | +func Debugf(format string, args ...interface{}) { |
| 141 | + _logrusEntry.Debugf(readIndentation() + format, args...) |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +func Info(args ...interface{}) { |
| 146 | + _logrusEntry.Info(readIndentation() + fmt.Sprint(args...)) |
| 147 | +} |
| 148 | + |
| 149 | +func Infof(format string, args ...interface{}) { |
| 150 | + _logrusEntry.Infof(readIndentation() + format, args...) |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +func Warn(args ...interface{}) { |
| 155 | + _logrusEntry.Warn(readIndentation() + fmt.Sprint(args...)) |
| 156 | +} |
| 157 | + |
| 158 | +func Warnf(format string, args ...interface{}) { |
| 159 | + _logrusEntry.Warnf(readIndentation() + format, args...) |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +func Error(args ...interface{}) { |
| 164 | + _logrusEntry.Error(readIndentation() + fmt.Sprint(args...)) |
| 165 | +} |
| 166 | + |
| 167 | +func Errorf(format string, args ...interface{}) { |
| 168 | + _logrusEntry.Errorf(readIndentation() + format, args...) |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +func Fatal(args ...interface{}) { |
| 173 | + _logrusEntry.Fatal(readIndentation() + fmt.Sprint(args...)) |
| 174 | +} |
| 175 | + |
| 176 | +func Fatalf(format string, args ...interface{}) { |
| 177 | + _logrusEntry.Fatalf(readIndentation() + format, args...) |
| 178 | +} |
| 179 | + |
| 180 | +func Panic(args ...interface{}) { |
| 181 | + _logrusEntry.Panic(readIndentation() + fmt.Sprint(args...)) |
| 182 | +} |
| 183 | + |
| 184 | +func Panicf(format string, args ...interface{}) { |
| 185 | + _logrusEntry.Panicf(readIndentation() + format, args...) |
| 186 | +} |
0 commit comments