From d2076e5a8dbc0db1008ac6f45fd6d6a158b054a5 Mon Sep 17 00:00:00 2001 From: Nils Stinnesbeck Date: Sun, 30 Apr 2023 20:57:06 +0200 Subject: [PATCH] first version --- LICENSE | 21 ++++++++++ README.md | 40 +++++++++++++++++++ go.mod | 11 ++++++ go.sum | 10 +++++ log.go | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++ log_test.go | 42 ++++++++++++++++++++ 6 files changed, 235 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 log.go create mode 100644 log_test.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d76a5e3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright Nils Stinnesbeck + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..10be5f0 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# log + +Package log provides a uniform method of logging. + +## how to use this? + +just import this module into your Go project by adding the following +line to your imports: +```go +log "git.stinnesbeck.com/go/log" +``` + +You can use this package in your code as follows: +```go +log.Print(LOGLEVEL, "This is your message of type LOGLEVEL") +log.PrintInfo("This is your info message") +log.PrintWarning("This is your warning message") +log.PrintError("This is your error message") +log.PrintDebug("This is your debug message") +``` + +## Log levels + +The following log levels are supported: + +| LogLevel | Name | +|----------|---------| +| 0 | ERROR | +| 1 | WARNING | +| 2 | INFO | +| 3 | DEBUG | + +The package will honor a globally set environment variable `LOGLEVEL`. The package will only print levels that are less or equal than the one provided (e.g. `WARNING` will only print `ERROR` and `WARNING`). If no environment variable is set or it can't be parsed into the names above, ERROR will be selected as the default. + +## Example usage + +You can see an example output by cloning this repo and executing: +```go +go test +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a2a2d7e --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module git.stinnesbeck.com/go/log + +go 1.20 + +require github.com/fatih/color v1.15.0 + +require ( + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect + golang.org/x/sys v0.6.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2624c9d --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= +github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/log.go b/log.go new file mode 100644 index 0000000..b578640 --- /dev/null +++ b/log.go @@ -0,0 +1,111 @@ +// Package log provides a uniform method of logging for +// all go projects at plusserver +package log + +import ( + "fmt" + "os" + "time" + + "github.com/fatih/color" +) + +// LogLevel is our custom type that represents the different log levels +type LogLevel int + +// Log levels that can be assigned to the WriteLog function +const ( + ERROR LogLevel = iota + WARNING + INFO + DEBUG +) + +// This implements the String representation for our custom type logLevel +func (l LogLevel) String() string { + switch l { + case WARNING: + return "WARNING" + case ERROR: + return "ERROR" + case INFO: + return "INFO" + default: + return "DEBUG" + } +} + +// colorOutput colors the output of a given message according to logLevel +func colorOutput(message string, logLevel LogLevel) { + switch logLevel { + case WARNING: + color.Yellow(message) + case ERROR: + color.Red(message) + case DEBUG: + color.HiBlack(message) + default: + color.Green(message) + } +} + +func parseLogLevel(s string) LogLevel { + switch s { + case "DEBUG": + return DEBUG + case "INFO": + return INFO + case "WARNING": + return WARNING + default: + return ERROR + } +} + +// Print writes the given text to the console and formats it properly. +// If no level is not provided or is unknown it will treat it as "INFO". +// +// Use the constants provided by this package for the available levels. +func Print(printLevel LogLevel, message ...any) { + // log level set by environment variable + var envLogLevel LogLevel + if l := os.Getenv("LOGLEVEL"); l != "" { + envLogLevel = parseLogLevel(l) + } + + // don't do anything if the requested print level is lower than the run level + if printLevel > envLogLevel { + return + } + + // get a timestamp for the log message + timeStamp := time.Now().Format("[2006-01-02 15:04:05]") + + // assemble the log message + logMessage := fmt.Sprintf("%s [%s]", timeStamp, printLevel) + for i := range message { + logMessage += " " + logMessage += fmt.Sprintf("%v", message[i]) + } + colorOutput(logMessage, printLevel) +} + +// PrintInfo is a Wrapper for Print(INFO, message) +func PrintInfo(message ...any) { + Print(INFO, message...) +} + +// PrintWarning is a Wrapper for Print(WARNING, message) +func PrintWarning(message ...any) { + Print(WARNING, message...) +} + +// PrintError is a Wrapper for Print(ERROR, message) +func PrintError(message ...any) { + Print(ERROR, message...) +} + +// PrintDebug is a Wrapper for Print(DEBUG, message) +func PrintDebug(message ...any) { + Print(DEBUG, message...) +} diff --git a/log_test.go b/log_test.go new file mode 100644 index 0000000..4ee7c97 --- /dev/null +++ b/log_test.go @@ -0,0 +1,42 @@ +package log + +import "testing" + +func TestPrint(t *testing.T) { + t.Setenv("LOGLEVEL", "DEBUG") + Print(DEBUG, "Testing DEBUG") + Print(DEBUG, "Testing DEBUG", 1, 2.3456, true) + Print(INFO, "Testing INFO") + Print(INFO, "Testing INFO", 1, 2.3456, true) + Print(WARNING, "Testing WARNING") + Print(WARNING, "Testing WARNING", 1, 2.3456, true) + Print(ERROR, "Testing ERROR") + Print(ERROR, "Testing ERROR", 1, 2.3456, true) +} + +func TestPrintDebug(t *testing.T) { + t.Setenv("LOGLEVEL", "DEBUG") + PrintDebug("Testing DEBUG") + PrintDebug("Testing DEBUG", 1, 2.3456, true) +} + +func TestPrintInfo(t *testing.T) { + t.Setenv("LOGLEVEL", "INFO") + PrintInfo("Testing INFO") + PrintInfo("Testing INFO", 1, 2.3456, true) + Print(DEBUG, "Testing DEBUG") +} + +func TestPrintWarning(t *testing.T) { + t.Setenv("LOGLEVEL", "WARNING") + PrintWarning("Testing WARNING") + PrintWarning("Testing WARNING", 1, 2.3456, true) + PrintInfo("Testing INFO") +} + +func TestPrintError(t *testing.T) { + t.Setenv("LOGLEVEL", "ERROR") + PrintError("Testing ERROR") + PrintError("Testing ERROR", 1, 2.3456, true) + PrintWarning("Testing WARNING") +}