feat: initial commit

This commit is contained in:
Simon Cornet 2025-03-06 11:25:38 +01:00
commit 693724d3b8
7 changed files with 116 additions and 0 deletions

21
LICENSE Normal file
View file

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

21
cmd/logger/debug.go Normal file
View file

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

25
cmd/logger/logging.go Normal file
View file

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

14
cmd/logger/prefix.go Normal file
View file

@ -0,0 +1,14 @@
package logging
var applicationName = "MyApp"
// Sets the application name prefix used in the logoutput. Example:
// <applicationName> | ...
func SetAppName(name string) {
applicationName = name
}
// Returns the logging prefix name as string.
func GetAppName() string {
return applicationName
}

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module logger
go 1.24.0

28
readme.md Normal file
View file

@ -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: <insert value of err>
```
# 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.

4
renovate.json Normal file
View file

@ -0,0 +1,4 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [ "local>cicd/renovate" ]
}