commit f5857eabc95aea271fb70b43e13d892d638bef7a Author: velvettear Date: Thu Nov 2 11:24:33 2023 +0100 initial commit diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..d87bf7b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,12 @@ +{ + "version": "0.0.1", + "configurations": [ + { + "name": "loggo", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}/main.go", + }, + ] +} \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..43b85c9 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,20 @@ +# MIT License +**Copyright (c) 2023 Daniel Sommer \** + +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.** \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9ece84d --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# loggo + +a simple log module written in golang. + +`go get https://git.velvettear.de/velvettear/loggo.git` \ No newline at end of file diff --git a/format.go b/format.go new file mode 100644 index 0000000..71010cc --- /dev/null +++ b/format.go @@ -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 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3ae252a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module velvettear/loggo + +go 1.21 \ No newline at end of file diff --git a/loglevel.go b/loglevel.go new file mode 100644 index 0000000..f34e310 --- /dev/null +++ b/loglevel.go @@ -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 +} diff --git a/print.go b/print.go new file mode 100644 index 0000000..7eebccc --- /dev/null +++ b/print.go @@ -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) + } +}