Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Lnotimestamp flag #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 38 additions & 21 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const (
// Lshortfile modifies the logger output to include filename and line number
// of the logging callsite, e.g. main.go:123. Overrides Llongfile.
Lshortfile

// Lnotimestamp disables the timestamp.
Lnotimestamp
)

// Read logger flags from the LOGFLAGS environment variable. Multiple flags can
Expand All @@ -70,6 +73,8 @@ func init() {
defaultFlags |= Llongfile
case "shortfile":
defaultFlags |= Lshortfile
case "notimestamp":
defaultFlags |= Lnotimestamp
}
}
}
Expand Down Expand Up @@ -201,25 +206,29 @@ func itoa(buf *[]byte, i int, wid int) {
// Appends a header in the default format 'YYYY-MM-DD hh:mm:ss.sss [LVL] TAG: '.
// If either of the Lshortfile or Llongfile flags are specified, the file named
// and line number are included after the tag and before the final colon.
func formatHeader(buf *[]byte, t time.Time, lvl, tag string, file string, line int) {
year, month, day := t.Date()
hour, min, sec := t.Clock()
ms := t.Nanosecond() / 1e6

itoa(buf, year, 4)
*buf = append(*buf, '-')
itoa(buf, int(month), 2)
*buf = append(*buf, '-')
itoa(buf, day, 2)
*buf = append(*buf, ' ')
itoa(buf, hour, 2)
*buf = append(*buf, ':')
itoa(buf, min, 2)
*buf = append(*buf, ':')
itoa(buf, sec, 2)
*buf = append(*buf, '.')
itoa(buf, ms, 3)
*buf = append(*buf, " ["...)
func formatHeader(buf *[]byte, t *time.Time, lvl, tag string, file string, line int) {
if t != nil {
year, month, day := t.Date()
hour, min, sec := t.Clock()
ms := t.Nanosecond() / 1e6

itoa(buf, year, 4)
*buf = append(*buf, '-')
itoa(buf, int(month), 2)
*buf = append(*buf, '-')
itoa(buf, day, 2)
*buf = append(*buf, ' ')
itoa(buf, hour, 2)
*buf = append(*buf, ':')
itoa(buf, min, 2)
*buf = append(*buf, ':')
itoa(buf, sec, 2)
*buf = append(*buf, '.')
itoa(buf, ms, 3)
*buf = append(*buf, ' ')
}

*buf = append(*buf, "["...)
*buf = append(*buf, lvl...)
*buf = append(*buf, "] "...)
*buf = append(*buf, tag...)
Expand Down Expand Up @@ -263,7 +272,11 @@ func callsite(flag uint32) (string, int) {
// function and formatting the provided arguments using the default formatting
// rules.
func (b *Backend) print(lvl, tag string, args ...interface{}) {
t := time.Now() // get as early as possible
var t *time.Time
if b.flag&(Lnotimestamp) == 0 {
now := time.Now() // get as early as possible
t = &now
}

bytebuf := buffer()

Expand All @@ -290,7 +303,11 @@ func (b *Backend) print(lvl, tag string, args ...interface{}) {
// function and formatting the provided arguments according to the given format
// specifier.
func (b *Backend) printf(lvl, tag string, format string, args ...interface{}) {
t := time.Now() // get as early as possible
var t *time.Time
if b.flag&(Lnotimestamp) == 0 {
now := time.Now() // get as early as possible
t = &now
}

bytebuf := buffer()

Expand Down