|
@@ -0,0 +1,170 @@
|
|
|
+package log
|
|
|
+
|
|
|
+import (
|
|
|
+ "flag"
|
|
|
+ "fmt"
|
|
|
+ "io"
|
|
|
+ _log "log"
|
|
|
+ "os"
|
|
|
+ "strings"
|
|
|
+
|
|
|
+ "source.uit.com/mikevoets/inf-3200-1/tools/log/config"
|
|
|
+ "source.uit.com/mikevoets/inf-3200-1/tools/log/errors"
|
|
|
+)
|
|
|
+
|
|
|
+func log_file(p config.LogPrefix) *os.File {
|
|
|
+ os.MkdirAll(config.LOG_DIR, 0777)
|
|
|
+ f, err := os.OpenFile(
|
|
|
+ fmt.Sprintf("%s%c%s%s.log", config.LOG_DIR, os.PathSeparator, strings.ToLower(string(p)), config.FILE_EXTRA),
|
|
|
+ config.FILE_MASK,
|
|
|
+ config.FILE_PERM,
|
|
|
+ )
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ _log.Fatal(err.Error())
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ return f
|
|
|
+}
|
|
|
+
|
|
|
+func log_new(l config.LogLevel, h config.LogHandle, p config.LogPrefix) *_log.Logger {
|
|
|
+
|
|
|
+ if (config.LOG_LEVEL_FILE&l) == config.LOG_LEVEL_FILE && (config.LOG_LEVEL_OUTPUT&l) == config.LOG_LEVEL_OUTPUT {
|
|
|
+ f := log_file(p)
|
|
|
+ return _log.New(
|
|
|
+ io.MultiWriter(f, h),
|
|
|
+ fmt.Sprintf("%s ", p),
|
|
|
+ config.LOG_FORMAT,
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ if (config.LOG_LEVEL_FILE & l) == config.LOG_LEVEL_FILE {
|
|
|
+ f := log_file(p)
|
|
|
+ return _log.New(
|
|
|
+ f, fmt.Sprintf("%s ", p),
|
|
|
+ config.LOG_FORMAT,
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ if (config.LOG_LEVEL_OUTPUT & l) == config.LOG_LEVEL_OUTPUT {
|
|
|
+ return _log.New(
|
|
|
+ h, fmt.Sprintf("%s ", p),
|
|
|
+ config.LOG_FORMAT,
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ return _log.New(
|
|
|
+ config.DISCARD_HANDLE, fmt.Sprintf("%s ", p),
|
|
|
+ config.LOG_FORMAT,
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
+func init() {
|
|
|
+ flag.Var(&config.LOG_LEVEL_OUTPUT, "llo", "Minimum log level that will be written to stdout.")
|
|
|
+ flag.Var(&config.LOG_LEVEL_FILE, "llf", "Minimum log level that will be written to file")
|
|
|
+}
|
|
|
+
|
|
|
+func log(t config.LogLevel) *_log.Logger {
|
|
|
+ var l *_log.Logger
|
|
|
+ switch t {
|
|
|
+ case config.TRACE:
|
|
|
+ if config.Trace == nil {
|
|
|
+ config.Trace = log_new(t, config.HANDLE_TRACE, config.PREFIX_TRACE)
|
|
|
+ }
|
|
|
+ l = config.Trace
|
|
|
+ case config.INFO:
|
|
|
+ if config.Info == nil {
|
|
|
+ config.Info = log_new(t, config.HANDLE_INFO, config.PREFIX_INFO)
|
|
|
+ }
|
|
|
+ l = config.Info
|
|
|
+ case config.WARNING:
|
|
|
+ if config.Warning == nil {
|
|
|
+ config.Warning = log_new(t, config.HANDLE_WARNING, config.PREFIX_WARNING)
|
|
|
+ }
|
|
|
+ l = config.Warning
|
|
|
+ case config.ERROR:
|
|
|
+ if config.Error == nil {
|
|
|
+ config.Error = log_new(t, config.HANDLE_ERROR, config.PREFIX_ERROR)
|
|
|
+ }
|
|
|
+ l = config.Error
|
|
|
+ case config.PANIC:
|
|
|
+ if config.Panic == nil {
|
|
|
+ config.Panic = log_new(t, config.HANDLE_ERROR, config.PREFIX_PANIC)
|
|
|
+ }
|
|
|
+ l = config.Panic
|
|
|
+ }
|
|
|
+
|
|
|
+ if l == nil {
|
|
|
+ _log.Panic(errors.UNKNOWN_LOGGER.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ return l
|
|
|
+}
|
|
|
+
|
|
|
+func LLO() *config.LogLevel {
|
|
|
+ return &config.LOG_LEVEL_OUTPUT
|
|
|
+}
|
|
|
+
|
|
|
+func LLF() *config.LogLevel {
|
|
|
+ return &config.LOG_LEVEL_FILE
|
|
|
+}
|
|
|
+
|
|
|
+func Trace(m string) {
|
|
|
+ log(config.TRACE).Print(m)
|
|
|
+}
|
|
|
+
|
|
|
+func Tracef(f string, v ...interface{}) {
|
|
|
+ log(config.TRACE).Printf(f, v...)
|
|
|
+}
|
|
|
+
|
|
|
+func Traceln(v ...interface{}) {
|
|
|
+ log(config.TRACE).Println(v...)
|
|
|
+}
|
|
|
+
|
|
|
+func Info(m string) {
|
|
|
+ log(config.INFO).Print(m)
|
|
|
+}
|
|
|
+
|
|
|
+func Infof(f string, v ...interface{}) {
|
|
|
+ log(config.INFO).Printf(f, v...)
|
|
|
+}
|
|
|
+
|
|
|
+func Infoln(v ...interface{}) {
|
|
|
+ log(config.INFO).Println(v...)
|
|
|
+}
|
|
|
+
|
|
|
+func Warning(m string) {
|
|
|
+ log(config.WARNING).Print(m)
|
|
|
+}
|
|
|
+
|
|
|
+func Warningf(f string, v ...interface{}) {
|
|
|
+ log(config.WARNING).Printf(f, v...)
|
|
|
+}
|
|
|
+
|
|
|
+func Warningln(v ...interface{}) {
|
|
|
+ log(config.WARNING).Println(v...)
|
|
|
+}
|
|
|
+func Error(m string) {
|
|
|
+ log(config.ERROR).Fatal(m)
|
|
|
+}
|
|
|
+
|
|
|
+func Errorf(f string, v ...interface{}) {
|
|
|
+ log(config.ERROR).Fatalf(f, v...)
|
|
|
+}
|
|
|
+
|
|
|
+func Errorln(v ...interface{}) {
|
|
|
+ log(config.ERROR).Fatalln(v...)
|
|
|
+}
|
|
|
+
|
|
|
+func Panic(m string) {
|
|
|
+ log(config.PANIC).Panic(m)
|
|
|
+}
|
|
|
+
|
|
|
+func Panicf(f string, v ...interface{}) {
|
|
|
+ log(config.PANIC).Panicf(f, v...)
|
|
|
+}
|
|
|
+
|
|
|
+func Panicln(v ...interface{}) {
|
|
|
+ log(config.PANIC).Panicln(v...)
|
|
|
+}
|