Skip to content

Commit

Permalink
errco: added milliseconds in log
Browse files Browse the repository at this point in the history
  • Loading branch information
gekigek99 committed Mar 9, 2023
1 parent 7347358 commit 46852dd
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/errco/errco.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"log"
"runtime"
"strings"
"time"
)

// DebugLvl specify the level of debugging
// (default is LVL_4 so it will log everything)
var DebugLvl LogLvl = LVL_4
// (start with LVL_3 to log config load errors)
var DebugLvl LogLvl = LVL_3

type MshLog struct {
Ori LogOri // log origin function
Expand Down Expand Up @@ -40,6 +41,11 @@ const (
COLOR_CYAN = "\033[36m"
)

func init() {
// disable log.Ldate and log.Ltime flags: will be set manually
log.SetFlags(0)
}

// NewLog returns a new msh log object.
//
// When a function fails and returns msh log using NewLog, msh log type must be TYPE_ERR or TYPE_WAR.
Expand Down Expand Up @@ -99,14 +105,20 @@ func (logMsh *MshLog) Log(tracing bool) *MshLog {

// -------- operations on copied log --------

// set mex string colors depending on logMod level
var (
typ string // log line: type of log
ori string // log line: origin of log
mex string // log line: message of log
cod string // log line: code of log
)

// set mex string depending on logMod level
switch logMod.Lvl {
case LVL_0: // make important logs more visible
logMod.Mex = COLOR_CYAN + logMod.Mex + COLOR_RESET
}

// set typ, ori, mex, cod strings depending on logMod type
var typ, ori, mex, cod string
switch logMod.Typ {
case TYPE_INF:
typ = fmt.Sprintf("%s%-6s%s", COLOR_BLUE, string(logMod.Typ), COLOR_RESET)
Expand Down Expand Up @@ -135,7 +147,8 @@ func (logMsh *MshLog) Log(tracing bool) *MshLog {
cod = fmt.Sprintf(" [%06x]", logMod.Cod)
}

log.Printf("[%s%-4s] %s%s%s\n",
log.Printf("%s [%s%-4s] %s%s%s\n",
time.Now().Format("2006/01/02 15:04:05.000"),
typ,
strings.Repeat("≡", 4-int(logMod.Lvl)),
ori,
Expand Down

0 comments on commit 46852dd

Please sign in to comment.