first version

This commit is contained in:
Nils Stinnesbeck 2023-04-30 20:57:06 +02:00
commit d2076e5a8d
Signed by: nils
GPG Key ID: C52E07422A136076
6 changed files with 235 additions and 0 deletions

21
LICENSE Normal file
View File

@ -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.

40
README.md Normal file
View File

@ -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
```

11
go.mod Normal file
View File

@ -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
)

10
go.sum Normal file
View File

@ -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=

111
log.go Normal file
View File

@ -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...)
}

42
log_test.go Normal file
View File

@ -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")
}