commit 693724d3b8c4f1184cff24e36e07c78c5be23219 Author: Simon Cornet Date: Thu Mar 6 11:25:38 2025 +0100 feat: initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8aba74c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Simon Cornet + +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/cmd/logger/debug.go b/cmd/logger/debug.go new file mode 100644 index 0000000..942ed2c --- /dev/null +++ b/cmd/logger/debug.go @@ -0,0 +1,21 @@ +package logging + +import ( + "io" + "log" +) + +var debug bool + +// Enables debug logging if set to true. Default is false. +func SetDebug(debugSetting bool) { + debug = debugSetting + if !debug { + log.SetOutput(io.Discard) + } +} + +// Returns the current debug value as a boolean. +func GetDebug() bool { + return debug +} diff --git a/cmd/logger/logging.go b/cmd/logger/logging.go new file mode 100644 index 0000000..7a7a587 --- /dev/null +++ b/cmd/logger/logging.go @@ -0,0 +1,25 @@ +package logging + +import ( + "log" +) + +// Prints the formatted log, taking both a message (string) and optionally +// an error as inputs. +func Print(message string, err error) { + if debug { + log.Printf(applicationName+" | LOG: %v\n", message) + if err != nil { + log.Printf(applicationName+" | ERROR: %v\n", err) + } + } +} + +// Prints the fatal error and exits the application. Takes both the message and +// optionally an error as inputs. +func Fatal(message string, err error) { + log.Fatalf(applicationName+" | FATAL: %v\n", message) + if err != nil { + log.Fatalf(applicationName+" | ERROR: %v\n", err) + } +} diff --git a/cmd/logger/prefix.go b/cmd/logger/prefix.go new file mode 100644 index 0000000..b9bdd59 --- /dev/null +++ b/cmd/logger/prefix.go @@ -0,0 +1,14 @@ +package logging + +var applicationName = "MyApp" + +// Sets the application name prefix used in the logoutput. Example: +// | ... +func SetAppName(name string) { + applicationName = name +} + +// Returns the logging prefix name as string. +func GetAppName() string { + return applicationName +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ba3151d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module logger + +go 1.24.0 diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..90e182a --- /dev/null +++ b/readme.md @@ -0,0 +1,28 @@ +# Logger + +This simple Golang package can be used for common logging functionality. + +# Options + +``` +logger.SetAppName("todo-app") # optional; default "MyApp" +logger.SetDebug(true) # optional; default false +``` + +# Usage examples +``` +logger.Print("Simple log message withouth error", nil) +logger.Fatal("Fatal log message with error", err) + +``` +Will be printed like this: +``` +2025/12/01 11:17:35 todo-app | LOG: Simple log message withouth error +2025/12/01 11:17:35 todo-app | FATAL: Fatal log message with error +2025/12/01 11:17:35 todo-app | ERROR: +``` + +# Return functions +``` +logger.GetAppName() will return the current used app name as a string. +logger.GetDebug() will return the current debug setting as a bool. diff --git a/renovate.json b/renovate.json new file mode 100644 index 0000000..56d1f2b --- /dev/null +++ b/renovate.json @@ -0,0 +1,4 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "extends": [ "local>cicd/renovate" ] +}