go_logger

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

go-logger

Yet another Go logger library.

How to use

  1. Import the library
import (
        gologger "github.com/randlabs/go-logger"
)
  1. Then use gologger.Create to create a logger object with desired options.
  2. Optionally, you can also use the default logger which outputs only to console by accesing gologger.Default().

Logger options:

The Options struct accepts several modifiers that affects the logger behavior:

Field Meaning
DisableConsole Disable console output.
FileLog Enable file logging. Optional. Details below.
SysLog Enable SysLog logging. Optional. Details below.
Level Set the initial logging level to use.
DebugLevel Set the initial logging level for debug output to use.
UseLocalTime Use the local computer time instead of UTC.
ErrorHandler A callback to call if an internal error is encountered.

The FileOptions struct accepts the following parameters:

Field Meaning
Prefix Filename prefix to use when a file is created. Defaults to the binary name.
Directory Destination directory to store log files.
DaysToKeep Amount of days to keep old logs,

And the SysLogOptions struct accepts the following parameters:

Field Meaning
AppName Application name to use. Defaults to the binary name.
Host Syslog server host name.
Port Syslog server port. Defaults to 514, 1468 or 6514 depending on the network protocol used.
UseTcp Use TCP instead of UDP.
UseTls Uses a secure connection. Implies TCP.
UseRFC5424 Send messages in the new RFC 5424 format instead of the original RFC 3164 specification.
MaxMessageQueueSize Set the maximum amount of messages to keep in memory if connection to the server is lost.
TlsConfig An optional pointer to a tls.Config object to provide the TLS configuration for use.

Example

package example

import (
    "fmt"
    "os"
    "path/filepath"

    gologger "github.com/randlabs/go-logger"
)

// Define a custom JSON message. Timestamp and level will be automatically added by the logger.
type JsonMessage struct {
    Message string `json:"message"`
}

func main() {
    // Create the logger
    logger, err := gologger.Create(gologger.Options{
        FileLog: &gologger.FileOptions{
            Directory:  "./logs",
            DaysToKeep: 7,
        },
        Level:        gologger.LogLevelDebug,
        DebugLevel:   1,
    })
    if err != nil {
        // Use default logger to send the error
        gologger.Default().Error(fmt.Sprintf("unable to initialize. [%v]", err))
        return
    }
    // Defer logger shut down
    defer logger.Destroy()

    // Send some logs using the plain text format 
    logger.Error("This is an error message sample")
    logger.Warning("This is a warning message sample")
    logger.Info("This is an information message sample")
    logger.Debug(1, "This is a debug message sample at level 1 which should be printed")
    logger.Debug(2, "This is a debug message sample at level 2 which should NOT be printed")

    // Send some other logs using the JSON format 
    logger.Error(JsonMessage{
        Message: "This is an error message sample",
    })
    logger.Warning(JsonMessage{
        Message: "This is a warning message sample",
    })
    logger.Info(JsonMessage{
        Message: "This is an information message sample",
    })
    logger.Debug(1, JsonMessage{
        Message: "This is a debug message sample at level 1 which should be printed",
    })
    logger.Debug(2, JsonMessage{
        Message: "This is a debug message sample at level 2 which should NOT be printed",
    })
}

License

See LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorHandler added in v1.2.0

type ErrorHandler func(message string)

ErrorHandler is a callback to call if an internal error must be notified.

type FileOptions added in v1.2.0

type FileOptions file.Options

FileOptions specifies the file logger settings.

type LogLevel added in v1.2.0

type LogLevel uint

LogLevel defines the level of message verbosity.

const (
	LogLevelQuiet   LogLevel = 0
	LogLevelError   LogLevel = 1
	LogLevelWarning LogLevel = 2
	LogLevelInfo    LogLevel = 3
	LogLevelDebug   LogLevel = 4
)

type Logger added in v1.2.0

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

Logger is the object that controls logging.

func Create added in v1.2.0

func Create(options Options) (*Logger, error)

Create creates a new logger.

func Default added in v1.2.0

func Default() *Logger

Default returns a logger that only outputs error and warnings to the console.

func (*Logger) Debug added in v1.2.0

func (logger *Logger) Debug(level uint, obj interface{})

Debug emits a debug message into the configured targets. If a string is passed, output format will be in DATE [LEVEL] MESSAGE. If a struct is passed, output will be in json with level and timestamp fields automatically added.

func (*Logger) Destroy added in v1.2.0

func (logger *Logger) Destroy()

Destroy shuts down the logger.

func (*Logger) Error added in v1.2.0

func (logger *Logger) Error(obj interface{})

Error emits an error message into the configured targets. If a string is passed, output format will be in DATE [LEVEL] MESSAGE. If a struct is passed, output will be in json with level and timestamp fields automatically added.

func (*Logger) Info added in v1.2.0

func (logger *Logger) Info(obj interface{})

Info emits an information message into the configured targets. If a string is passed, output format will be in DATE [LEVEL] MESSAGE. If a struct is passed, output will be in json with level and timestamp fields automatically added.

func (*Logger) SetDebugLevel added in v1.2.0

func (logger *Logger) SetDebugLevel(newLevel uint)

SetDebugLevel sets the minimum level for debug messages.

func (*Logger) SetLevel added in v1.2.0

func (logger *Logger) SetLevel(newLevel LogLevel)

SetLevel sets the minimum level for all messages.

func (*Logger) Warning added in v1.2.0

func (logger *Logger) Warning(obj interface{})

Warning emits a warning message into the configured targets. If a string is passed, output format will be in DATE [LEVEL] MESSAGE. If a struct is passed, output will be in json with level and timestamp fields automatically added.

type Options

type Options struct {
	// Disable console output.
	DisableConsole bool `json:"disableConsole,omitempty"`

	// Optionally enable file logging and establish its settings.
	FileLog *FileOptions `json:"fileLog,omitempty"`

	// Optionally enable syslog logging and establish its settings.
	SysLog *SyslogOptions `json:"sysLog,omitempty"`

	// Set the initial logging level to use.
	Level LogLevel `json:"level,omitempty"`

	// Set the initial logging level for debug output to use.
	DebugLevel uint `json:"debugLevel,omitempty"`

	// Use the local computer time instead of UTC.
	UseLocalTime bool `json:"useLocalTime,omitempty"`

	// A callback to call if an internal error is encountered.
	ErrorHandler ErrorHandler
}

Options specifies the logger settings to use when initialized.

type SyslogOptions added in v1.2.0

type SyslogOptions syslog.Options

SyslogOptions specifies the syslog logger settings.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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