hcl

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2022 License: MIT Imports: 10 Imported by: 11

README

go-hcl GocodecovGo Report CardReleaseGoDoc

hcl is a replacement for log which wraps hc-log

hcl is supposed to provide advanced but painless logging

Features

  • 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

Example

package main


import (
	"log"
	"os"

	"github.com/hashicorp/go-hclog"
	"github.com/vogtp/go-hcl"
)

func main() {
	// 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.Info("I am visible when started by go run")
	hcl.Warn("I am visible when started by go build")
	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")
}


Output:

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 [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 build
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

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

Examples

Constants

View Source
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 GetWriter

func GetWriter() io.Writer

GetWriter returns a writer to be used for frameworks to output to log

func Info

func Info(msg string, args ...interface{})

Info logs a message and key/value pairs at the INFO level

func Infof

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

Infof provides printf like logging to Info

func IsDebug

func IsDebug() bool

IsDebug indicates if Debug logs would be written

func IsError

func IsError() bool

IsError indicates if Error logs would be written

func IsGoRun

func IsGoRun() bool

IsGoRun checks if run by go run it does this by checking arg[0]

func IsGoTest

func IsGoTest() bool

IsGoTest checks if run by go test it does this by checking arg[0]

func IsInfo

func IsInfo() bool

IsInfo indicates if Info logs would be written

func IsTrace

func IsTrace() bool

IsTrace indicates if Trace logs would be written

func IsWarn

func IsWarn() bool

IsWarn indicates if Warn logs would be written

func Print

func Print(v ...interface{})

Print works like Print from stdlib logs to Info

func Printf

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

Printf works like Printf from stdlib logs to Info

func Println

func Println(v ...interface{})

Println works like hcl.Print logs to Info

func SetLevel

func SetLevel(level hclog.Level)

SetLevel sets the log level

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

func Warn

func Warn(msg string, args ...interface{})

Warn logs a message and key/value pairs at the WARN level

func Warnf

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

Warnf provides printf like logging to Warn

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

func LibraryLogger(name string) Logger

LibraryLogger creates a logger for libraries if the library used hcl it creates a sublogger otherwise it mimics stdlib

func Named

func Named(name string) Logger

Named creates a sublogger with the name appended to the old name

func New

func New(opts ...LoggerOpt) Logger

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 ResetNamed

func ResetNamed(name string) Logger

ResetNamed creates a logger with a new name

func (Logger) Debugf

func (l Logger) Debugf(format string, v ...interface{})

Debugf provides printf like logging to Debug

func (Logger) Errorf

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

Errorf provides printf like logging to Error

func (Logger) GetExecutableName added in v0.2.1

func (Logger) GetExecutableName() string

GetExecutableName extracts the name of the executable removes path and suffix

func (Logger) GetWriter

func (l Logger) GetWriter() io.Writer

GetWriter returns a writer to be used for frameworks to output to log

func (Logger) Infof

func (l Logger) Infof(format string, v ...interface{})

Infof provides printf like logging to Info

func (Logger) IsGoRun

func (Logger) IsGoRun() bool

IsGoRun checks if run by go run it does this by checking arg[0]

func (Logger) IsGoTest

func (Logger) IsGoTest() bool

IsGoTest checks if run by go test it does this by checking arg[0]

func (Logger) Named

func (l Logger) Named(name string) Logger

Named creates a sublogger with the name appended to the old name

func (Logger) Print

func (l Logger) Print(v ...interface{})

Print works like Print from stdlib logs to Info

func (Logger) Printf

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

Printf works like Printf 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

func (l Logger) ResetNamed(name string) Logger

ResetNamed creates a logger with a new name

func (*Logger) SetLevel

func (l *Logger) SetLevel(level hclog.Level)

SetLevel sets the log level

func (*Logger) SetWriter

func (l *Logger) SetWriter(w io.Writer)

SetWriter sets the write of this logger redirects the std lib log

func (Logger) Tracef

func (l Logger) Tracef(format string, v ...interface{})

Tracef provides printf like logging to Trace

func (Logger) Vlog added in v0.3.0

func (l Logger) Vlog() *vlog.Logger

func (Logger) Warnf

func (l Logger) Warnf(format string, v ...interface{})

Warnf provides printf like logging to Warn

func (Logger) With

func (l Logger) With(args ...interface{}) Logger

With sreates a sublogger that will always have the given key/value pairs

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 WithName added in v0.2.0

func WithName(name string) LoggerOpt

WithName sets the name of the logger

func WithStdlib added in v0.2.0

func WithStdlib(b bool) LoggerOpt

WithStdlib controlls if stdlib logger should be changed

func WithWriter

func WithWriter(w io.Writer) LoggerOpt

WithWriter is used to create a logger with a custom writer

Jump to

Keyboard shortcuts

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