Skip to content

Simple Go package for logging messages to a file and console with different log levels.

License

Notifications You must be signed in to change notification settings

zewebdev1337/logln

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

logln

A simple Go package for logging messages to a file and console with different log levels.

Installation

go get github.com/zewebdev1337/logln

Usage

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

Log Levels

The following log levels are available:

  • 0: INFO
  • 1: WARNING
  • 2: ERROR
  • 3: FATAL
  • 4: PANIC
  • 5: DEBUG

Functions

Init()

Initializes the log file.

Close()

Closes the log file.

Logln(line string, level int, isSilent bool)

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.

Printf(text string, level int, isSilent bool)

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.

ManualLogf(text string, level int, isSilent bool)

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.

PrintErrorOrSuccessIfNotOk(ok bool, msg string, successLevel int, isSuccessSilent bool)

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.

PrintFatalOrSuccessIfNotOk(ok bool, msg string, successLevel int, isSuccessSilent bool)

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.

PrintPanicOrSuccessIfNotOk(ok bool, msg string, successLevel int, isSuccessSilent bool)

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.

PrintErrorOrSuccess(msg string, err error, successLevel int, isSuccessSilent bool)

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.

PrintFatalOrSuccess(msg string, err error, successLevel int, isSuccessSilent bool)

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.

PrintPanicOrSuccess(msg string, err error, successLevel int, isSuccessSilent bool)

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.

License

This package is licensed under the MIT License.

About

Simple Go package for logging messages to a file and console with different log levels.

Topics

Resources

License

Stars

Watchers

Forks

Languages