log

package module
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: MIT Imports: 6 Imported by: 11

README

IMQS Logging for Go applications

See documentation

Updated to go-1.22.7 for tag

Documentation

Overview

Package log is a standard logging system for IMQS Go applications.

This is a very thin wrapper around lumberjack. What this package provides is a consistent log format, with predefined severity levels.

Usage

Create a new logger using log.New(filename, runtime.GOOS != "windows"). You can write to it using the various logging methods. 'filename' may also be log.Stdout or log.Stderr, in which case we do the obvious thing.

If you need to forward other log messages to this system, then Forwarder might have sufficient functionality to achieve that.

Index

Constants

View Source
const (
	Stdout  = "stdout"
	Stderr  = "stderr"
	Testing = ".testing."
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Forwarder

type Forwarder struct {
	StripPrefixLen int     // Number of bytes of prefix to strip (typically the timestamp from the incoming log message)
	Level          Level   // The log level assigned to all messages from this source
	Target         *Logger // The destination
}

Forwards log messages to an existing Logger, while performing some sanitizing which ensures that all log messages share the same format

func NewForwarder

func NewForwarder(stripPrefixLen int, level Level, target *Logger) *Forwarder

Create a new log forwarder

func (*Forwarder) Write

func (f *Forwarder) Write(p []byte) (n int, err error)

type Level

type Level int
const (
	Trace Level = iota
	Debug
	Info
	Warn
	Error
)

func ParseLevel

func ParseLevel(lev string) (Level, error)

Parse a level string such as "info" or "warn". Only the first character of the string is considered.

type Logger

type Logger struct {
	Level Level // Log messages with a level lower than this are discarded. Default level is Info
	// contains filtered or unexported fields
}

A logger object. Use New() to construct one.

var Log *Logger

This variable is initialized the first time New is called and then returned on every subsequent call. It can be used throughout an application by just importing this package into any package

func New

func New(filename string, logToStdout bool) *Logger

New creates a new logger. If logToStdout is true all logs will be written to both the specified file and stdout. Filename may also be one of the special names log.Stdout and log.Stderr. If log.Stdout is specified and logToStdout is also set to true then the logs will only be written to stdout.

func NewTesting

func NewTesting(t *testing.T) *Logger

NewTesting creates a logger object that emits logs to the given Testing context

func (*Logger) Close

func (l *Logger) Close() error

Close attempts to close the connection

func (*Logger) Debug

func (l *Logger) Debug(msg string)

func (*Logger) Debugf

func (l *Logger) Debugf(format string, params ...interface{})

func (*Logger) Error

func (l *Logger) Error(msg string)

func (*Logger) Errorf

func (l *Logger) Errorf(format string, params ...interface{})

func (*Logger) Info

func (l *Logger) Info(msg string)

func (*Logger) Infof

func (l *Logger) Infof(format string, params ...interface{})

func (*Logger) Log

func (l *Logger) Log(level Level, msg string)

func (*Logger) Logf

func (l *Logger) Logf(level Level, format string, params ...interface{})

func (*Logger) Trace

func (l *Logger) Trace(msg string)

func (*Logger) Tracef

func (l *Logger) Tracef(format string, params ...interface{})

func (*Logger) Warn

func (l *Logger) Warn(msg string)

func (*Logger) Warnf

func (l *Logger) Warnf(format string, params ...interface{})

func (*Logger) Write

func (l *Logger) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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