From d548d8683eebc911ab26b31da648955bfcfe3635 Mon Sep 17 00:00:00 2001 From: Francesco Pessolano Date: Wed, 27 Jan 2021 09:28:29 +0100 Subject: [PATCH] Version v.0.3.2 --- CHANGELOG.md | 4 ++++ README.md | 4 ++++ mlogger.go | 29 +++++++++++++++++++++++------ mlogger_test.go | 2 ++ 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45bb958..014b1d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All notable changes to this project will be documented in this file. +## [0.3.2] +#### Added +- Verbose flag + ## [0.2.2] #### Changed - Removed setup assigment causing an irremevant race warning diff --git a/README.md b/README.md index d82a9d2..b9af036 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,10 @@ The logfile is formatted with the methods: mlogger.SetTextLimit(logId, lm, li, ll) Where _lm_, _li_ and _ll_ are the number of maximum characters to be used for the message text, id and level. If 0 is given, no restriction will be used. +The logs can also be set to echo all written liones to the console by togglingthe verbose flag: + + mlogger.Verbose({true|false}) + **Usage** A log line can be stored by using a method associated to a given level (Log, Info, Error, Warning, Recovered amd Panic). For example: diff --git a/mlogger.go b/mlogger.go index e8dfd4a..20a9edb 100644 --- a/mlogger.go +++ b/mlogger.go @@ -47,6 +47,7 @@ var loggerChan chan logMessage var once sync.Once var consoleLog = false var index = 0 +var verbose = false // DeclareLog is used to declare a new log of name 'fn'. If 'dt' is true the name will become fn_current date.logfile. // Furthermore, a new logfile will be created everyday. Otherwise it will be fn.logfile and will not be created everyday. @@ -87,6 +88,11 @@ func Close() (e error) { return } +// Enables verbose (affects all logs) +func Verbose(v bool) { + verbose = v +} + // SetTextLimit sets formatting limits for message (lm), id (li) and level (ll) in number of characters for logfile tag func SetTextLimit(tag, lm, li, ll int) error { lock.Lock() @@ -158,7 +164,7 @@ func SetError(lg int, es string, id string, e error, data []int, aggregate bool) // logger is the core thread handling all the writings to log files func logger(data chan logMessage) { - r := func(file logfile, olddate string, d logMessage, dt ...[]int) (msg string) { + logEntryGenerator := func(file logfile, olddate string, d logMessage, dt ...[]int) (msg string) { date := time.Now().Format("Mon Jan:_2 15:04 2006") if file.messageLength != 0 { if len(d.msg.Message) > file.messageLength { @@ -214,6 +220,14 @@ func logger(data chan logMessage) { return } + consoleEntryGenerator := func(d logMessage) (msg string) { + date := time.Now().Format("Mon Jan:_2 15:04 2006") + msg = d.level + " -- " + d.msg.Id + ": " + d.msg.Message + "" + msg = date + " -- " + msg + msg = strings.Trim(msg, " ") + return + } + defer func() { if e := recover(); e != nil { if consoleLog { @@ -229,13 +243,16 @@ func logger(data chan logMessage) { if d.id < index { file := declaredLogs[d.id] lock.RUnlock() + if verbose { + fmt.Println(consoleEntryGenerator(d)) + } if input, err := ioutil.ReadFile(file.filename); err != nil { if fn, err := os.Create(file.filename); err != nil { if consoleLog { fmt.Println("support.logger: error creating log: ", err) } } else { - if _, err := fn.WriteString(r(file, "", d) + "\n"); err != nil { + if _, err := fn.WriteString(logEntryGenerator(file, "", d) + "\n"); err != nil { if consoleLog { fmt.Println("support.logger: error creating log: ", err) } @@ -262,15 +279,15 @@ func logger(data chan logMessage) { } else { cd := logMessage{d.id, "System Warning", LoggerData{"logger", "error converting accruing data from log " + d.msg.Id, []int{}, false}} - newC += r(file, "", cd) + "\n" - newC += r(file, "", d) + "\n" + newC += logEntryGenerator(file, "", cd) + "\n" + newC += logEntryGenerator(file, "", d) + "\n" skip = true adFile = false break mainloop } } if !skip { - newC += r(file, spv[0], d, nd) + "\n" + newC += logEntryGenerator(file, spv[0], d, nd) + "\n" adFile = false } } else { @@ -282,7 +299,7 @@ func logger(data chan logMessage) { } } if adFile { - newC += r(file, "", d) + "\n" + newC += logEntryGenerator(file, "", d) + "\n" } if err = ioutil.WriteFile(file.filename, []byte(newC), 0644); err != nil { log.Println("support.logger: error writing log: ", err) diff --git a/mlogger_test.go b/mlogger_test.go index db09143..95d6df4 100644 --- a/mlogger_test.go +++ b/mlogger_test.go @@ -16,9 +16,11 @@ func Test_DLogger(t *testing.T) { fmt.Println(e) os.Exit(0) } + Verbose(true) Log(logId, LoggerData{"test1", "testing message", []int{2}, true}) Error(logId, LoggerData{"test1", "testing message", []int{2}, true}) Info(logId, LoggerData{"test1", "testing message", []int{2}, false}) + Verbose(false) Warning(logId, LoggerData{"test1", "testing message", []int{2}, true}) Recovered(logId, LoggerData{"test1", "testing message", []int{2}, true}) Panic(logId, LoggerData{"test1", "testing message", []int{}, true}, false)