logger

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2022 License: MIT Imports: 7 Imported by: 2

README

Logger

A customisable logger for Go.

Installation

go get -u github.com/alexgokhale/logger

Examples

This logger offers both default Printf style methods, and a New method to create a new logger with a prefix of your choice.

One key difference between these methods and standard Printf is that a trailing newline is added to the end of the string printed.

Default Methods
name := "Alex"

// Print a critical message (coloured red)
// logger.Fatal acts identically to logger.Critical but calls `os.Exit(1)`
logger.Critical("%s broke something", name)
// Output: [CRIT] [2022-04-01T20:35:20.987] Alex broke something

// Print a warning message (coloured yellow)
logger.Warning("User '%s' missing, skipping entry", name)
// Output: [WARN] [2022-04-01T20:35:20.987] User 'Alex' missing, skipping entry

// Print an informational message (coloured blue)
logger.Info("Connecting to database...")
// Output: [INFO] [2022-04-01T20:35:20.987] Connecting to database...

// Print a success message (coloured green)
logger.Success("Connected successfully!")
// Output: [INFO] [2022-04-01T20:35:20.987] Connected successfully!

// Print a generic log message
logger.Log("Testing %d %d %d!", 1, 2, 3)
// Output: [2022-04-01T20:35:20.987] Testing 1 2 3!
Custom Logger
databaseFile, err := os.Create("database.txt")

if err != nil {
  panic(err)
}

databaseLogger := logger.New("<DATABASE>", databaseFile)

databaseLogger.Info("Connecting...")
// Output: [INFO] [2022-04-01T20:35:20.987] <DATABASE> Connecting...
databaseLogger.Success("Connected successfully!")
// Output: [INFO] [2022-04-01T20:35:20.987] <DATABASE> Connected successfully!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Critical

func Critical(format string, a ...interface{})

func Fatal

func Fatal(format string, a ...interface{})

func Info

func Info(format string, a ...interface{})

func Log

func Log(format string, a ...interface{})

func Success

func Success(format string, a ...interface{})

func Warning

func Warning(format string, a ...interface{})

Types

type Logger

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

func New

func New(prefix string, file *os.File) *Logger

func (*Logger) Critical

func (l *Logger) Critical(format string, a ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(format string, a ...interface{})

func (*Logger) Info

func (l *Logger) Info(format string, a ...interface{})

func (*Logger) Log

func (l *Logger) Log(format string, a ...interface{})

func (*Logger) Success

func (l *Logger) Success(format string, a ...interface{})

func (*Logger) Warning

func (l *Logger) Warning(format string, a ...interface{})

Jump to

Keyboard shortcuts

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