log

package
v0.12.2 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2022 License: BSD-3-Clause, MIT, MIT-0, + 1 more Imports: 11 Imported by: 0

README

log - uniformly configurable loggers

go get -u "tawesoft.co.uk/go"
import "tawesoft.co.uk/go/log"
Links License Stable?
homedocssrc MIT-0 candidate

About

Package log provides a common way to quickly configure a logging implementation with file rotation, syslog, console output, etc. for some popular logging implementations such as zerolog.

This package defines the configuration interface, which is json-encodable.

Loggers are concretely implemented by the packages in the subfolder e.g. tawesoft.co.uk/go/log/zerolog.

The package also wraps the stdlib syslog as an interface without it being a compile-time constraint so that it can be imported on platforms that don't support syslog (like Windows), giving a runtime error if used instead.

Examples

package main

import (
    "encoding/json"
    "fmt"
    "time"

    "tawesoft.co.uk/go/log"
)

func main() {
    cfg := log.Config{
        Syslog: log.ConfigSyslog{
            Enabled:  true,
            Network:  "", // local
            Address:  "", // local
            Priority: log.LOG_ERR | log.LOG_DAEMON,
            Tag:      "example",
        },
        File:   log.ConfigFile{
            Enabled:          true,
            Mode:             0600,
            Path:             "example.log",
            Rotate:           true,
            RotateCompress:   true,
            RotateMaxSize:    64 * 1024 * 1024, // 64MB
            RotateKeepAge:    30 * 24 * time.Hour,
            RotateKeepNumber: 32, // 32 * 64 MB = 2 GB max storage (before compression)
        },
        Stderr: log.ConfigStderr{
            Enabled: true,
            Color:   true,
        },
    }

    encodedCfg, err := json.Marshal(cfg)
    fmt.Printf("Encoded config: %s\n\n", string(encodedCfg))

    var decodedCfg log.Config
    err = json.Unmarshal(encodedCfg, &decodedCfg)
    if err != nil { panic(err) }
    fmt.Printf("Decoded encoded config: %+v\n", decodedCfg)

    if cfg != decodedCfg { panic("not equal!") }
}

Getting Help

This package is part of tawesoft.co.uk/go, a monorepo for small Go modules maintained by Tawesoft®. Check out that URL for more information about other Go modules from Tawesoft plus community and commercial support options.

Documentation

Overview

Package log provides a common way to quickly configure a logging implementation with file rotation, syslog, console output, etc. for some popular logging implementations such as zerolog.

This package defines the configuration interface, which is json-encodable.

Loggers are concretely implemented by the packages in the subfolder e.g. tawesoft.co.uk/go/log/zerolog.

The package also wraps the stdlib syslog as an interface without it being a compile-time constraint so that it can be imported on platforms that don't support syslog (like Windows), giving a runtime error if used instead.

Examples

https://www.tawesoft.co.uk/go/doc/log/examples/encode/

Package Information

License: MIT-0 (see LICENSE.txt)

Stable: candidate

For more information, documentation, source code, examples, support, links, etc. please see https://www.tawesoft.co.uk/go and https://www.tawesoft.co.uk/go/log

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Syslog ConfigSyslog
	File   ConfigFile
	Stderr ConfigStderr
}

type ConfigFile

type ConfigFile struct {
	Enabled bool

	// Mode to use when creating the file e.g. 0644, 0600
	Mode os.FileMode

	// Path to write the current (non-rotated) file. Rotated files appear
	// in the same directory.
	Path string

	// If Rotate is true, logs are rotated (e.g. like logrotate) once they
	// get to a certain size.
	Rotate bool

	// If RotateCompress is true, rotated log files are compressed (read them
	// with zless, zcat, or gunzip, for example)
	RotateCompress bool

	// A log is rotated if it would be bigger than RotateMaxSize (in bytes)
	RotateMaxSize int // bytes

	// If non-zero, delete any rotated logs older than RotateKeepAge
	RotateKeepAge time.Duration

	// If non-zero, keep only this many rotated logs and delete any exceeding
	// the limit of RotateKeepNumber.
	RotateKeepNumber int
}

ConfigFile configures a file logger, with optional file rotation

func (ConfigFile) MarshalJSON added in v0.8.0

func (c ConfigFile) MarshalJSON() ([]byte, error)

func (*ConfigFile) UnmarshalJSON added in v0.8.0

func (c *ConfigFile) UnmarshalJSON(data []byte) error

type ConfigStderr

type ConfigStderr struct {
	Enabled bool

	// If Color is true, output is colourised iff Stderr is attached to
	// a terminal.
	Color bool
}

ConfigStdio configures a logger which writes to Stderr

func (ConfigStderr) ShouldColorize added in v0.6.0

func (c ConfigStderr) ShouldColorize(output *os.File) bool

ShouldColorize returns true if the output should be colourised (if possible) for a given output (e.g. os.Stderr). This is true when both the config Color field is true and the output is a terminal.

type ConfigSyslog

type ConfigSyslog struct {
	Enabled  bool
	Network  string   // See syslog.Dial
	Address  string   // See syslog.Dial
	Priority Priority // See syslog.Dial
	Tag      string   // See syslog.Dial
}

ConfigSyslog configures a syslog logger

func (ConfigSyslog) Dial

func (c ConfigSyslog) Dial() (Syslog, error)

Dial connects to a local or remote syslog.

func (ConfigSyslog) MarshalJSON added in v0.8.0

func (c ConfigSyslog) MarshalJSON() ([]byte, error)

func (*ConfigSyslog) UnmarshalJSON added in v0.8.0

func (c *ConfigSyslog) UnmarshalJSON(data []byte) error

type Priority

type Priority int

Priority is a combination of the syslog facility and severity. See https://pkg.go.dev/log/syslog#Priority

const (
	LOG_EMERG Priority = iota
	LOG_ALERT
	LOG_CRIT
	LOG_ERR
	LOG_WARNING
	LOG_NOTICE
	LOG_INFO
	LOG_DEBUG
)
const (
	LOG_KERN Priority = iota << 3
	LOG_USER
	LOG_MAIL
	LOG_DAEMON
	LOG_AUTH
	LOG_SYSLOG // should only ever be used internally by syslogd
	LOG_LPR
	LOG_NEWS
	LOG_UUCP
	LOG_CRON
	LOG_AUTHPRIV
	LOG_FTP

	LOG_LOCAL0
	LOG_LOCAL1
	LOG_LOCAL2
	LOG_LOCAL3
	LOG_LOCAL4
	LOG_LOCAL5
	LOG_LOCAL6
	LOG_LOCAL7
)

func ParsePriority

func ParsePriority(str string) (Priority, error)

func (Priority) String

func (p Priority) String() string

type Syslog

type Syslog interface {
	io.Writer
	Close() error
	Debug(m string) error
	Info(m string) error
	Warning(m string) error
	Err(m string) error
	Emerg(m string) error
	Crit(m string) error
}

Syslog is syslog.Writer as an interface

Directories

Path Synopsis
examples
Package log/zerolog makes it trivial to configure a zerolog logger with syslog, rotating file, and/or console output using the same uniform configuration interface.
Package log/zerolog makes it trivial to configure a zerolog logger with syslog, rotating file, and/or console output using the same uniform configuration interface.

Jump to

Keyboard shortcuts

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