plogger

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2025 License: AGPL-3.0 Imports: 11 Imported by: 72

README

Plogger

Pretendo Network logger library

Logs

Plogger supports 5 log levels:

  • Critical - Used to indicate something has gone horribly wrong
  • Error - A general error has occured
  • Warning - Something is likely not right, but not overly important
  • Success - Success
  • Info - A general log message

All log messages are formatted using the following format: [time] [level] spacing [function_name package_name/file_name:file_line] : message. The spacing is used to align sections

For example:

[2025-02-16T17:22:50] [WARNING]  [func 0() main.init/init.go:34] : Error loading .env file: open .env: no such file or directory
[2025-02-16T17:22:50] [ERROR]    [func 0() main.init/init.go:52] : PN_FRIENDS_CONFIG_DATABASE_URI environment variable not set

API

Creating a logger

To create a logger, call plogger.NewLogger() with an optional base path. If no path is given, . is used. When the logger writes to a file, the file path BASE/log/TYPE.log is written to

var logger = plogger.NewLogger() // Writes log files to ./log/TYPE.log
var logger = plogger.NewLogger("./some/other/base") // Writes log files to ./some/other/base/log/TYPE.log
Logging a message
logger.Critical("Log message") // Creates a "Critical" level log message
logger.Error("Log message") // Creates a "Error" level log message
logger.Warning("Log message") // Creates a "Warning" level log message
logger.Success("Log message") // Creates a "Success" level log message
logger.Info("Log message") // Creates a "Info" level log message

logger.Criticalf("Log %s", "message") // Creates a "Critical" level log message, with additional formatting
logger.Errorf("Log %s", "message") // Creates a "Error" level log message, with additional formatting
logger.Warningf("Log %s", "message") // Creates a "Warning" level log message, with additional formatting
logger.Successf("Log %s", "message") // Creates a "Success" level log message, with additional formatting
logger.Infof("Log %s", "message") // Creates a "Info" level log message, with additional formatting
Opt-out of logging

By default all loggers will write logs to both the console, to the log levels log file, and the all.log file. To opt-out of a type of logging, configure the logger after creation:

logger.SetLogToStdOut(false) // Disables console logging for this specific logger
logger.SetLogToFile(false) // Disables file logging for this specific logger

plogger.SetGlobalLogToStdOut(false) // Disables console logging for all loggers
plogger.SetGlobalLogToFile(false) // Disables file logging for all loggers

The global settings may also be set using environment variables:

PLOGGER_DISABLE_CONSOLE_LOGGING_GLOBAL=true # Disables console logging for all loggers
PLOGGER_DISABLE_FILE_LOGGING_GLOBAL=true # Disables file logging for all loggers

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GlobalLogToFile added in v1.1.0

func GlobalLogToFile() bool

func GlobalLogToStdOut added in v1.1.0

func GlobalLogToStdOut() bool

func SetGlobalLogToFile added in v1.1.0

func SetGlobalLogToFile(enable bool)

func SetGlobalLogToStdOut added in v1.1.0

func SetGlobalLogToStdOut(enable bool)

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger(args ...string) *Logger

func (*Logger) Critical

func (logger *Logger) Critical(message string)

func (*Logger) Criticalf added in v1.0.3

func (logger *Logger) Criticalf(message string, a ...any)

func (*Logger) Error

func (logger *Logger) Error(message string)

func (*Logger) Errorf added in v1.0.3

func (logger *Logger) Errorf(message string, a ...any)

func (*Logger) Info

func (logger *Logger) Info(message string)

func (*Logger) Infof added in v1.0.3

func (logger *Logger) Infof(message string, a ...any)

func (*Logger) LogToFile added in v1.1.0

func (logger *Logger) LogToFile() bool

func (*Logger) LogToStdOut added in v1.1.0

func (logger *Logger) LogToStdOut() bool

func (*Logger) SetLogToFile added in v1.1.0

func (logger *Logger) SetLogToFile(enable bool)

func (*Logger) SetLogToStdOut added in v1.1.0

func (logger *Logger) SetLogToStdOut(enable bool)

func (*Logger) Success

func (logger *Logger) Success(message string)

func (*Logger) Successf added in v1.0.3

func (logger *Logger) Successf(message string, a ...any)

func (*Logger) Warning

func (logger *Logger) Warning(message string)

func (*Logger) Warningf added in v1.0.3

func (logger *Logger) Warningf(message string, a ...any)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL