feat: split logging package into different files
This commit is contained in:
parent
cf79badd52
commit
b6b2704636
3 changed files with 41 additions and 36 deletions
21
cmd/gogitlabber/logging/debug.go
Normal file
21
cmd/gogitlabber/logging/debug.go
Normal 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
|
||||||
|
}
|
||||||
|
|
@ -1,56 +1,25 @@
|
||||||
package logging
|
package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// define application name used in prefix
|
|
||||||
var applicationName = ""
|
|
||||||
|
|
||||||
func SetAppName(name string) {
|
|
||||||
applicationName = name
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAppName() string {
|
|
||||||
return applicationName
|
|
||||||
}
|
|
||||||
|
|
||||||
var debug bool
|
|
||||||
|
|
||||||
func SetDebug(debugSetting bool) {
|
|
||||||
debug = debugSetting
|
|
||||||
if !debug {
|
|
||||||
log.SetOutput(io.Discard)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetDebug() bool {
|
|
||||||
return debug
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prints the formatted log, taking both a message (string) and optionally
|
// Prints the formatted log, taking both a message (string) and optionally
|
||||||
// an error as inputs.
|
// an error as inputs.
|
||||||
func Print(message string, err error) error {
|
func Print(message string, err error) {
|
||||||
if debug {
|
if debug {
|
||||||
|
log.Printf(applicationName+" | LOG: %v\n", message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf(applicationName+" | DEBUG: %v error: %v\n", message, err)
|
log.Printf(applicationName+" | ERROR: %v\n", err)
|
||||||
}
|
}
|
||||||
if err == nil {
|
|
||||||
log.Printf(applicationName+" | DEBUG: %v\n", message)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return fmt.Errorf("It seems you want to print a log while debug is off")
|
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the fatal error and exits the application. Takes both the message and
|
// Prints the fatal error and exits the application. Takes both the message and
|
||||||
// optionally an error as inputs.
|
// optionally an error as inputs.
|
||||||
func Fatal(message string, err error) {
|
func Fatal(message string, err error) {
|
||||||
if err != nil {
|
|
||||||
log.Fatalf(applicationName+" | FATAL: %v error: %v\n", message, err)
|
|
||||||
}
|
|
||||||
log.Fatalf(applicationName+" | FATAL: %v\n", message)
|
log.Fatalf(applicationName+" | FATAL: %v\n", message)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf(applicationName+" | ERROR: %v\n", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
15
cmd/gogitlabber/logging/prefix.go
Normal file
15
cmd/gogitlabber/logging/prefix.go
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
package logging
|
||||||
|
|
||||||
|
var applicationName = ""
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue