A simple Go package for logging messages to a file and console with different log levels.
go get github.com/zewebdev1337/logln
package main
import (
"fmt"
"github.com/zewebdev1337/logln"
)
func main() {
// Initialize the log file
logln.Init()
defer logln.Close()
// Log an info message
logln.Logln("This is an info message", 0, false)
// Output: 2006/01/02 15:04:05 INFO This is an info message
// Log a warning message
logln.Logln("This is a warning message", 1, false)
// Output: 2006/01/02 15:04:05 WARNING This is a warning message
// Log a debug message
logln.Logln("This is a debug message", 5, false)
// Output: 2006/01/02 15:04:05 DEBUG This is a debug message
// Log a debug message (to file only)
logln.Logln("This is a debug message", 5, true)
// Output: 2006/01/02 15:04:05 DEBUG This is a debug message
// Log an warning if the condition is false
logln.PrintErrorOrSuccessIfNotOk(ok, "something is not ok", 0, false)
// Output: 2006/01/02 15:04:05 WARNING something is not ok.
// Log an error if the condition is false
logln.PrintErrorOrSuccessIfNotOk(ok, "something is not ok", 0, false)
// Output: 2006/01/02 15:04:05 ERROR An error ocurred: something is not ok.
// Log a fatal error if the condition is false and exit
logln.PrintFatalOrSuccessIfNotOk(ok, "something is fatally not ok", 0, false)
// Output: 2006/01/02 15:04:05 FATAL Fatal error encountered: something is fatally not ok.
// Log a panic if the condition is false and panic
logln.PrintPanicOrSuccessIfNotOk(ok, "something is not ok", 0, false)
// Output: 2006/01/02 15:04:05 FATAL Panic: something is not ok.
// Log an error if the error is not nil
err := fmt.Errorf("this is an error")
logln.PrintErrorOrSuccess("trying to do something", err, 0, false)
// Output: 2006/01/02 15:04:05 ERROR An error occurred trying to do something: this is an error.
logln.PrintWarningOrSuccess("this error triggered this warning", err, 0, false)
// Output: 2006/01/02 15:04:05 WARNING this error triggered this warning: this is an error.
// Log a fatal error if the error is not nil and exit
logln.PrintFatalOrSuccess("reading essential data", err, 0, false)
// Output: 2006/01/02 15:04:05 FATAL A fatal error encountered reading essential data: this is an error.
// Log a panic if the error is not nil and panic
logln.PrintPanicOrSuccess("expecting something different", err, 0, false)
// Output: 2006/01/02 15:04:05 FATAL Panic expecting something different: this is an error.
// Log a success if the error is nil
err := nil
logln.PrintErrorOrSuccess("trying to do something", err, 5, false)
// Output: 2006/01/02 15:04:05 DEBUG Success trying to do something.
}
The following log levels are available:
0
: INFO1
: WARNING2
: ERROR3
: FATAL4
: PANIC5
: DEBUG
Initializes the log file.
Closes the log file.
Logs a message with the given level and message.
line
: The message to log.level
: The log level.isSilent
: Whether to suppress the message from the console output.
Logs a message without appending a newline.
text
: The message to log.level
: The log level.isSilent
: Whether to suppress the message from the console output.
Logs a message without appending a newline and without the date and time prefix.
text
: The message to log.level
: The log level.isSilent
: Whether to suppress the message from the console output.
Logs an error message if ok
is false, otherwise logs a success message.
ok
: The condition to check.msg
: The message to log.successLevel
: The log level for the success message.isSuccessSilent
: Whether to suppress the success message from the console output.
Logs a fatal error message if ok
is false and exits, otherwise logs a success message.
ok
: The condition to check.msg
: The message to log.successLevel
: The log level for the success message.isSuccessSilent
: Whether to suppress the success message from the console output.
Logs a panic message if ok
is false and panics, otherwise logs a success message.
ok
: The condition to check.msg
: The message to log.successLevel
: The log level for the success message.isSuccessSilent
: Whether to suppress the success message from the console output.
Logs an error message if err
is not nil, otherwise logs a success message.
msg
: The message to log.err
: The error to check.successLevel
: The log level for the success message.isSuccessSilent
: Whether to suppress the success message from the console output.
Logs a fatal error message if err
is not nil and exits, otherwise logs a success message.
msg
: The message to log.err
: The error to check.successLevel
: The log level for the success message.isSuccessSilent
: Whether to suppress the success message from the console output.
Logs a panic message if err
is not nil and panics, otherwise logs a success message.
msg
: The message to log.err
: The error to check.successLevel
: The log level for the success message.isSuccessSilent
: Whether to suppress the success message from the console output.
This package is licensed under the MIT License.