initial commit
This commit is contained in:
commit
f5857eabc9
7 changed files with 211 additions and 0 deletions
12
.vscode/launch.json
vendored
Normal file
12
.vscode/launch.json
vendored
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"version": "0.0.1",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "loggo",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "auto",
|
||||||
|
"program": "${workspaceFolder}/main.go",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
20
LICENSE.md
Normal file
20
LICENSE.md
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
# MIT License
|
||||||
|
**Copyright (c) 2023 Daniel Sommer \<daniel.sommer@velvettear.de\>**
|
||||||
|
|
||||||
|
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 (including the next
|
||||||
|
paragraph) 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.**
|
5
README.md
Normal file
5
README.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
# loggo
|
||||||
|
|
||||||
|
a simple log module written in golang.
|
||||||
|
|
||||||
|
`go get https://git.velvettear.de/velvettear/loggo.git`
|
55
format.go
Normal file
55
format.go
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
package loggo
|
||||||
|
|
||||||
|
// timestamp placeholder
|
||||||
|
const PLACEHOLDER_TIMESTAMP = "$TIMESTAMP$"
|
||||||
|
|
||||||
|
// log level placeholder
|
||||||
|
const PLACEHOLDER_LOGLEVEL = "$LOGLEVEL$"
|
||||||
|
|
||||||
|
// message placeholder
|
||||||
|
const PLACEHOLDER_MESSAGE = "$MESSAGE$"
|
||||||
|
|
||||||
|
// time difference placeholder
|
||||||
|
const PLACEHOLDER_TIMEDIFF = "$TIMEDIFF$"
|
||||||
|
|
||||||
|
// extras placeholder
|
||||||
|
const PLACEHOLDER_EXTRAS = "$EXTRAS$"
|
||||||
|
|
||||||
|
// current log format (defaults to: "$TIMESTAMP$ - [$LOGLEVEL$] > $MESSAGE ($EXTRAS$) [$TIMEDIFF$]")
|
||||||
|
var logFormat = "$TIMESTAMP$ - [$LOGLEVEL$] > $MESSAGE ($EXTRAS$) [$TIMEDIFF$]"
|
||||||
|
|
||||||
|
// current date format (defaults to: "02.01.2006 15:04:05")
|
||||||
|
var dateFormat = "02.01.2006 15:04:05"
|
||||||
|
|
||||||
|
// current extras separator (defaults to: "|")
|
||||||
|
var extrasSeparator = "|"
|
||||||
|
|
||||||
|
// get current log format
|
||||||
|
func GetLogFormat() string {
|
||||||
|
return logFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
// set current prefix format
|
||||||
|
func SetLogFormat(format string) {
|
||||||
|
logFormat = format
|
||||||
|
}
|
||||||
|
|
||||||
|
// get current date format
|
||||||
|
func GetDateFormat() string {
|
||||||
|
return dateFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
// set current date format
|
||||||
|
func SetDateFormat(format string) {
|
||||||
|
dateFormat = format
|
||||||
|
}
|
||||||
|
|
||||||
|
// get current extras separator
|
||||||
|
func GetExtrasSeparator() string {
|
||||||
|
return extrasSeparator
|
||||||
|
}
|
||||||
|
|
||||||
|
// set current extras separator
|
||||||
|
func SetExtrasSeparator(separator string) {
|
||||||
|
extrasSeparator = separator
|
||||||
|
}
|
3
go.mod
Normal file
3
go.mod
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
module velvettear/loggo
|
||||||
|
|
||||||
|
go 1.21
|
35
loglevel.go
Normal file
35
loglevel.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package loggo
|
||||||
|
|
||||||
|
// struct for log levels
|
||||||
|
type logLevel struct {
|
||||||
|
level int
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// debug log level (level = 0, name = "debug")
|
||||||
|
var DebugLevel = logLevel{0, "debug"}
|
||||||
|
|
||||||
|
// info log level (level = 1, name = "info")
|
||||||
|
var InfoLevel = logLevel{1, "info"}
|
||||||
|
|
||||||
|
// warning log level (level = 2, name = "warning")
|
||||||
|
var WarningLevel = logLevel{2, "warning"}
|
||||||
|
|
||||||
|
// error log level (level = 3, name = "error")
|
||||||
|
var ErrorLevel = logLevel{3, "error"}
|
||||||
|
|
||||||
|
// fatal log level (level = 4, name = "fatal")
|
||||||
|
var FatalLevel = logLevel{4, "fatal"}
|
||||||
|
|
||||||
|
// current log level (defaults to: "infoLevel")
|
||||||
|
var currentLevel = InfoLevel
|
||||||
|
|
||||||
|
// return current set log level
|
||||||
|
func GetLogLevel() logLevel {
|
||||||
|
return currentLevel
|
||||||
|
}
|
||||||
|
|
||||||
|
// set current log level
|
||||||
|
func SetLogLevel(level logLevel) {
|
||||||
|
currentLevel = level
|
||||||
|
}
|
81
print.go
Normal file
81
print.go
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
package loggo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// debug logging without time difference calculation
|
||||||
|
func Debug(message string, extras ...string) {
|
||||||
|
print(DebugLevel, message, -1, extras...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// debug logging with time difference calculation
|
||||||
|
func DebugTimed(message string, timestamp int64, extras ...string) {
|
||||||
|
print(DebugLevel, message, timestamp, extras...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// info logging without time difference calculation
|
||||||
|
func Info(message string, extras ...string) {
|
||||||
|
print(InfoLevel, message, -1, extras...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// info logging with time difference calculation
|
||||||
|
func InfoTimed(message string, timestamp int64, extras ...string) {
|
||||||
|
print(InfoLevel, message, timestamp, extras...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// warning logging without time difference calculation
|
||||||
|
func Warning(message string, extras ...string) {
|
||||||
|
print(WarningLevel, message, -1, extras...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// warning logging with time difference calculation
|
||||||
|
func WarningTimed(message string, timestamp int64, extras ...string) {
|
||||||
|
print(WarningLevel, message, timestamp, extras...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// error logging without time difference calculation
|
||||||
|
func Error(message string, extras ...string) {
|
||||||
|
print(ErrorLevel, message, -1, extras...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// error logging with time difference calculation
|
||||||
|
func ErrorTimed(message string, timestamp int64, extras ...string) {
|
||||||
|
print(ErrorLevel, message, timestamp, extras...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// fatal logging without time difference calculation
|
||||||
|
func Fatal(message string, extras ...string) {
|
||||||
|
print(FatalLevel, message, -1, extras...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// fatal logging with time difference calculation
|
||||||
|
func FatalTimed(message string, timestamp int64, extras ...string) {
|
||||||
|
print(FatalLevel, message, timestamp, extras...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// core logging to console
|
||||||
|
func print(logLevel logLevel, message string, timestamp int64, extras ...string) {
|
||||||
|
if len(message) == 0 || logLevel.level < GetLogLevel().level {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
formatted := GetLogFormat()
|
||||||
|
formatted = strings.ReplaceAll(formatted, PLACEHOLDER_TIMESTAMP, now.Format(GetDateFormat()))
|
||||||
|
formatted = strings.ReplaceAll(formatted, PLACEHOLDER_LOGLEVEL, logLevel.name)
|
||||||
|
formatted = strings.ReplaceAll(formatted, PLACEHOLDER_MESSAGE, message)
|
||||||
|
if len(extras) > 0 {
|
||||||
|
formatted = strings.ReplaceAll(formatted, PLACEHOLDER_EXTRAS, strings.Join(extras, GetExtrasSeparator()))
|
||||||
|
}
|
||||||
|
if timestamp >= 0 {
|
||||||
|
formatted = strings.ReplaceAll(formatted, PLACEHOLDER_TIMEDIFF, strconv.FormatInt(now.UnixMilli()-timestamp, 10)+"ms")
|
||||||
|
}
|
||||||
|
fmt.Println(formatted)
|
||||||
|
if logLevel.level == FatalLevel.level {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue