Categorygithub.com/vogtp/go-hcl
modulepackage
0.3.0
Repository: https://github.com/vogtp/go-hcl.git
Documentation: pkg.go.dev

# 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

# Functions

Debug logs a message and key/value pairs at the DEBUG level.
Debugf provides printf like logging to Debug.
Error log a message and key/value pairs at the ERROR level.
Errorf provides printf like logging to Error.
GetExecutableName extracts the name of the executable removes path and suffix.
GetWriter returns a writer to be used for frameworks to output to log.
Info logs a message and key/value pairs at the INFO level.
Infof provides printf like logging to Info.
IsDebug indicates if Debug logs would be written.
IsError indicates if Error logs would be written.
IsGoRun checks if run by go run it does this by checking arg[0].
IsGoTest checks if run by go test it does this by checking arg[0].
IsInfo indicates if Info logs would be written.
IsTrace indicates if Trace logs would be written.
IsWarn indicates if Warn logs would be written.
LibraryLogger creates a logger for libraries if the library used hcl it creates a sublogger otherwise it mimics stdlib.
Named creates a sublogger with the name appended to the old name.
New constructs a new logger loglevel is Error if build and info if `go run` std lib logging is redirected.
Print works like Print from stdlib logs to Info.
Printf works like Printf from stdlib logs to Info.
Println works like hcl.Print logs to Info.
ResetNamed creates a logger with a new name.
SetLevel sets the log level.
Trace logs a message and key/value pairs at the TRACE level.
Tracef provides printf like logging to Trace.
Warn logs a message and key/value pairs at the WARN level.
Warnf provides printf like logging to Warn.
WithLevel is used to create a logger with log level.
WithLoggerOptions sets the logger options of hclog Name, Level, Output get overwritter by hcl options.
WithName sets the name of the logger.
WithStdlib controlls if stdlib logger should be changed.
WithWriter is used to create a logger with a custom writer.

# Constants

TimeFormat is the default time formating of hcl.

# Structs

Logger implements hclog.Logger.

# Type aliases

LoggerOpt is a func to set opts at logger creation.