log

package
v1.32.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2022 License: MIT Imports: 6 Imported by: 15

Documentation

Overview

Package log provides a global logger for zlog.

Example

This example uses command-line flags to demonstrate various outputs depending on the chosen log level.

package main

import (
	"flag"
	"os"
	"time"

	"git.tuxpa.in/a/zlog"
	"git.tuxpa.in/a/zlog/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zlog.TimeFieldFormat = ""

	zlog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}
	log.Logger = zlog.New(os.Stdout).With().Timestamp().Logger()
}

func main() {
	setup()
	debug := flag.Bool("debug", false, "sets log level to debug")

	flag.Parse()

	// Default level for this example is info, unless debug flag is present
	zlog.SetGlobalLevel(zlog.InfoLevel)
	if *debug {
		zlog.SetGlobalLevel(zlog.DebugLevel)
	}

	log.Debug().Msg("This message appears only when log level set to Debug")
	log.Info().Msg("This message appears when log level set to Debug or Info")

	if e := log.Debug(); e.Enabled() {
		// Compute log output only if enabled.
		value := "bar"
		e.Str("foo", value).Msg("some debug message")
	}

}
Output:

{"level":"info","time":1199811905,"message":"This message appears when log level set to Debug or Info"}

Index

Examples

Constants

This section is empty.

Variables

View Source
var Logger = zlog.New(os.Stderr).With().Timestamp().Logger()

Logger is the global logger.

Functions

func Ctx

func Ctx(ctx context.Context) *zlog.Logger

Ctx returns the Logger associated with the ctx. If no logger is associated, a disabled logger is returned.

func Debug

func Debug() *zlog.Event

Debug starts a new message with debug level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "debug")

package main

import (
	"os"
	"time"

	"git.tuxpa.in/a/zlog"
	"git.tuxpa.in/a/zlog/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zlog.TimeFieldFormat = ""

	zlog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}
	log.Logger = zlog.New(os.Stdout).With().Timestamp().Logger()
}

func main() {
	setup()
	log.Debug().Msg("hello world")

}
Output:

{"level":"debug","time":1199811905,"message":"hello world"}

func Err added in v1.30.0

func Err(err error) *zlog.Event

Err starts a new message with error level with err as a field if not nil or with info level if err is nil.

You must call Msg on the returned event in order to send the event.

Example

Example of a conditional level based on the presence of an error.

package main

import (
	"errors"
	"os"
	"time"

	"git.tuxpa.in/a/zlog"
	"git.tuxpa.in/a/zlog/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zlog.TimeFieldFormat = ""

	zlog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}
	log.Logger = zlog.New(os.Stdout).With().Timestamp().Logger()
}

func main() {
	setup()
	err := errors.New("some error")
	log.Err(err).Msg("hello world")
	log.Err(nil).Msg("hello world")

}
Output:

{"level":"error","error":"some error","time":1199811905,"message":"hello world"}
{"level":"info","time":1199811905,"message":"hello world"}

func Error

func Error() *zlog.Event

Error starts a new message with error level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "error")

package main

import (
	"os"
	"time"

	"git.tuxpa.in/a/zlog"
	"git.tuxpa.in/a/zlog/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zlog.TimeFieldFormat = ""

	zlog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}
	log.Logger = zlog.New(os.Stdout).With().Timestamp().Logger()
}

func main() {
	setup()
	log.Error().Msg("hello world")

}
Output:

{"level":"error","time":1199811905,"message":"hello world"}

func Errorf added in v1.30.0

func Errorf(format string, v ...interface{})

Errorf sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Errorf.

func Errorln added in v1.30.0

func Errorln(args ...interface{})

func Fatal

func Fatal() *zlog.Event

Fatal starts a new message with fatal level. The os.Exit(1) function is called by the Msg method.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "fatal")

package main

import (
	"errors"
	"os"
	"time"

	"git.tuxpa.in/a/zlog"
	"git.tuxpa.in/a/zlog/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zlog.TimeFieldFormat = ""

	zlog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}
	log.Logger = zlog.New(os.Stdout).With().Timestamp().Logger()
}

func main() {
	setup()
	err := errors.New("A repo man spends his life getting into tense situations")
	service := "myservice"

	log.Fatal().
		Err(err).
		Str("service", service).
		Msgf("Cannot start %s", service)

	// Outputs: {"level":"fatal","time":1199811905,"error":"A repo man spends his life getting into tense situations","service":"myservice","message":"Cannot start myservice"}
}
Output:

func Fatalf added in v1.30.0

func Fatalf(format string, args ...interface{})

func Fatalln added in v1.30.0

func Fatalln(args ...interface{})

func Hook added in v1.4.0

func Hook(h zlog.Hook) zlog.Logger

Hook returns a logger with the h Hook.

func Info

func Info() *zlog.Event

Info starts a new message with info level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "info")

package main

import (
	"os"
	"time"

	"git.tuxpa.in/a/zlog"
	"git.tuxpa.in/a/zlog/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zlog.TimeFieldFormat = ""

	zlog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}
	log.Logger = zlog.New(os.Stdout).With().Timestamp().Logger()
}

func main() {
	setup()
	log.Info().Msg("hello world")

}
Output:

{"level":"info","time":1199811905,"message":"hello world"}

func Level

func Level(level zlog.Level) zlog.Logger

Level creates a child logger with the minimum accepted level set to level.

func Log

func Log() *zlog.Event

Log starts a new message with no level. Setting zlog.GlobalLevel to zlog.Disabled will still disable events produced by this method.

You must call Msg on the returned event in order to send the event.

Example

Example of a log with no particular "level"

package main

import (
	"os"
	"time"

	"git.tuxpa.in/a/zlog"
	"git.tuxpa.in/a/zlog/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zlog.TimeFieldFormat = ""

	zlog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}
	log.Logger = zlog.New(os.Stdout).With().Timestamp().Logger()
}

func main() {
	setup()
	log.Log().Msg("hello world")

}
Output:

{"time":1199811905,"message":"hello world"}

func Output added in v1.2.0

func Output(w io.Writer) zlog.Logger

Output duplicates the global logger and sets w as its output.

func Panic

func Panic() *zlog.Event

Panic starts a new message with panic level. The message is also sent to the panic function.

You must call Msg on the returned event in order to send the event.

func Panicf added in v1.30.0

func Panicf(format string, args ...interface{})

func Panicln added in v1.30.0

func Panicln(args ...interface{})

func Print added in v1.3.0

func Print(v ...interface{})

Print sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Print.

Example

Simple logging example using the Print function in the log package Note that both Print and Printf are at the debug log level by default

package main

import (
	"os"
	"time"

	"git.tuxpa.in/a/zlog"
	"git.tuxpa.in/a/zlog/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zlog.TimeFieldFormat = ""

	zlog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}
	log.Logger = zlog.New(os.Stdout).With().Timestamp().Logger()
}

func main() {
	setup()

	log.Print("hello world")
}
Output:

{"level":"debug","time":1199811905,"message":"hello world"}

func Printf added in v1.3.0

func Printf(format string, v ...interface{})

Printf sends a log event using debug level and no extra field. Arguments are handled in the manner of fmt.Printf.

Example

Simple logging example using the Printf function in the log package

package main

import (
	"os"
	"time"

	"git.tuxpa.in/a/zlog"
	"git.tuxpa.in/a/zlog/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zlog.TimeFieldFormat = ""

	zlog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}
	log.Logger = zlog.New(os.Stdout).With().Timestamp().Logger()
}

func main() {
	setup()

	log.Printf("hello %s", "world")
}
Output:

{"level":"debug","time":1199811905,"message":"hello world"}

func Println added in v1.30.0

func Println(args ...interface{})

func Sample

func Sample(s zlog.Sampler) zlog.Logger

Sample returns a logger with the s sampler.

func Trace added in v1.30.0

func Trace() *zlog.Event

Trace starts a new message with trace level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "trace")

package main

import (
	"os"
	"time"

	"git.tuxpa.in/a/zlog"
	"git.tuxpa.in/a/zlog/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zlog.TimeFieldFormat = ""

	zlog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}
	log.Logger = zlog.New(os.Stdout).With().Timestamp().Logger()
}

func main() {
	setup()
	log.Trace().Msg("hello world")

}
Output:

{"level":"trace","time":1199811905,"message":"hello world"}

func Warn

func Warn() *zlog.Event

Warn starts a new message with warn level.

You must call Msg on the returned event in order to send the event.

Example

Example of a log at a particular "level" (in this case, "warn")

package main

import (
	"os"
	"time"

	"git.tuxpa.in/a/zlog"
	"git.tuxpa.in/a/zlog/log"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zlog.TimeFieldFormat = ""

	zlog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}
	log.Logger = zlog.New(os.Stdout).With().Timestamp().Logger()
}

func main() {
	setup()
	log.Warn().Msg("hello world")

}
Output:

{"level":"warn","time":1199811905,"message":"hello world"}

func With

func With() zlog.Context

With creates a child logger with the field added to its context.

func WithLevel added in v1.4.0

func WithLevel(level zlog.Level) *zlog.Event

WithLevel starts a new message with level.

You must call Msg on the returned event in order to send the event.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL