Skip to content

Commit

Permalink
Version v.0.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
fpessolano committed Jan 27, 2021
1 parent 7f33695 commit d548d86
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 23 additions & 6 deletions mlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions mlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d548d86

Please sign in to comment.