Documentation ¶
Overview ¶
Package hcl is a replacement for log which wraps hc-log
hcl is supposed to provide advanced but painless logging
- it offers simple package level functionality
- exports most (all?) of the hclog features
- it redirects stdlib log to itself.
- it does not support Fatal or Panic functions
Index ¶
- Constants
- func Debug(msg string, args ...interface{})
- func Debugf(format string, v ...interface{})
- func Error(msg string, args ...interface{})
- func Errorf(format string, v ...interface{})
- func GetExecutableName() string
- func GetWriter() io.Writer
- func Info(msg string, args ...interface{})
- func Infof(format string, v ...interface{})
- func IsDebug() bool
- func IsError() bool
- func IsGoRun() bool
- func IsGoTest() bool
- func IsInfo() bool
- func IsTrace() bool
- func IsWarn() bool
- func Print(v ...interface{})
- func Printf(format string, v ...interface{})
- func Println(v ...interface{})
- func SetLevel(level hclog.Level)
- func Trace(msg string, args ...interface{})
- func Tracef(format string, v ...interface{})
- func Warn(msg string, args ...interface{})
- func Warnf(format string, v ...interface{})
- type Logger
- func (l Logger) Debugf(format string, v ...interface{})
- func (l Logger) Errorf(format string, v ...interface{})
- func (Logger) GetExecutableName() string
- func (l Logger) GetWriter() io.Writer
- func (l Logger) Infof(format string, v ...interface{})
- func (Logger) IsGoRun() bool
- func (Logger) IsGoTest() bool
- func (l Logger) Named(name string) Logger
- func (l Logger) Print(v ...interface{})
- func (l Logger) Printf(format string, v ...interface{})
- func (l Logger) Println(v ...interface{})
- func (l Logger) ResetNamed(name string) Logger
- func (l *Logger) SetLevel(level hclog.Level)
- func (l *Logger) SetWriter(w io.Writer)
- func (l Logger) Tracef(format string, v ...interface{})
- func (l Logger) Vlog() *vlog.Logger
- func (l Logger) Warnf(format string, v ...interface{})
- func (l Logger) With(args ...interface{}) Logger
- type LoggerOpt
Examples ¶
Constants ¶
const (
// TimeFormat is the default time formating of hcl
TimeFormat = "2006/01/02 15:04:05"
)
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
func Debug(msg string, args ...interface{})
Debug logs a message and key/value pairs at the DEBUG level
func Debugf ¶
func Debugf(format string, v ...interface{})
Debugf provides printf like logging to Debug
func Error ¶
func Error(msg string, args ...interface{})
Error log a message and key/value pairs at the ERROR level
func Errorf ¶
func Errorf(format string, v ...interface{})
Errorf provides printf like logging to Error
func GetExecutableName ¶
func GetExecutableName() string
GetExecutableName extracts the name of the executable removes path and suffix
func Info ¶
func Info(msg string, args ...interface{})
Info logs a message and key/value pairs at the INFO level
func IsGoTest ¶
func IsGoTest() bool
IsGoTest checks if run by go test it does this by checking arg[0]
func Printf ¶
func Printf(format string, v ...interface{})
Printf works like Printf from stdlib logs to Info
func Trace ¶
func Trace(msg string, args ...interface{})
Trace logs a message and key/value pairs at the TRACE level
func Tracef ¶
func Tracef(format string, v ...interface{})
Tracef provides printf like logging to Trace
Types ¶
type Logger ¶
type Logger struct { hclog.Logger // contains filtered or unexported fields }
Logger implements hclog.Logger
Example ¶
// log to a logger named after the executable hcl.Print("I am a logger named after the executable") hcl.Printf("But I go rid of some parts in %s", os.Args[0]) hcl.Println("I log to Info") hcl.Errorf("I am getting %s bored", "really") hcl.Error("I got called with", "args", os.Args) hcl.Warn("I am visible in build code") hcl.Info("I am visible when started by go run") hcl.Warn("I am visible when started by go test") hcl.Trace("I am not visible") hcl.SetLevel(hclog.Trace) hcl.Trace("now you can see me") log.Printf("I look the same as %s", "hcl.Printf") //create a sublogger webLogger := hcl.Named("web") webLogger.Info("Start of web logs") // 2022-02-25T09:40:12+01:00 [INFO] hcl: I am a logger named after the executable // 2022-02-25T09:40:12+01:00 [INFO] hcl: But I go rid of some parts in /tmp/go-build2029833176/b001/hcl.test // 2022-02-25T09:40:12+01:00 [INFO] hcl: I log to Info // 2022-02-25T09:40:12+01:00 [ERROR] hcl: I am getting really bored // 2022-02-25T09:40:12+01:00 [ERROR] hcl: I got called with: args=["/tmp/go-build2029833176/b001/hcl.test", "-test.paniconexit0", "-test.timeout=10m0s"] // 2022-02-25T09:40:12+01:00 [WARN] hcl: I am visible in build code // 2022-02-25T09:40:12+01:00 [INFO] hcl: I am visible when started by go run // 2022-02-25T09:40:12+01:00 [WARN] hcl: I am visible when started by go test // 2022-02-25T09:40:12+01:00 [TRACE] hcl: now you can see me // 2022-02-25T09:40:12+01:00 [INFO] hcl: I look the same as hcl.Printf // 2022-02-25T09:40:12+01:00 [INFO] hcl.web: Start of web logs
Output:
func LibraryLogger ¶ added in v0.2.0
LibraryLogger creates a logger for libraries if the library used hcl it creates a sublogger otherwise it mimics stdlib
func New ¶
New constructs a new logger loglevel is Error if build and info if `go run` std lib logging is redirected
Example ¶
log := hcl.New() log.Info("I am a logger named after the application") log2 := hcl.ResetNamed("app-name") log2.Warnf("Mostly the same as above: %s", "but not as clear")
Output:
func (Logger) GetExecutableName ¶ added in v0.2.1
GetExecutableName extracts the name of the executable removes path and suffix
func (Logger) Print ¶
func (l Logger) Print(v ...interface{})
Print works like Print from stdlib logs to Info
func (Logger) Println ¶
func (l Logger) Println(v ...interface{})
Println works like hcl.Print logs to Info
func (Logger) ResetNamed ¶
ResetNamed creates a logger with a new name
type LoggerOpt ¶
type LoggerOpt func(*Logger)
LoggerOpt is a func to set opts at logger creation
func WithLevel ¶
func WithLevel(lvl hclog.Level) LoggerOpt
WithLevel is used to create a logger with log level
func WithLoggerOptions ¶ added in v0.2.0
func WithLoggerOptions(opts *hclog.LoggerOptions) LoggerOpt
WithLoggerOptions sets the logger options of hclog Name, Level, Output get overwritter by hcl options
func WithStdlib ¶ added in v0.2.0
WithStdlib controlls if stdlib logger should be changed
func WithWriter ¶
WithWriter is used to create a logger with a custom writer