glogger

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2023 License: MIT Imports: 6 Imported by: 1

README

glogger

codecov goreportcard pipeline

glogger is a Go package that provides a logger that can be used to write log messages to an output destination, such as the standard output or a file. It has various levels of log severity (e.g. debug, info, warning, error, fatal) and supports two formats: inline-string and JSON.

Installation

To install glogger, run:

go get github.com/psyb0t/glogger

Usage

The global variables output, logFormat, and logLevel control the output destination, format, and minimum severity level of log messages, respectively. You can set these variables using the functions SetOutput, SetLogFormat, and SetLogLevel.

To create a new logger, use the New function and pass in a Caller struct that contains the caller information (e.g. service, package, receiver, function). The Logger interface provides methods for logging messages at different severity levels: Debug, Info, Warn, Error, and Fatal.

The Err, RequestID, TraceID, SpanID, and Data methods can be used to add extra data to the log entry. The Err method sets the error field in the log entry to the given value. The RequestID, TraceID, and SpanID methods set the corresponding fields in the log entry to the given values. The Data method adds a data field to the log entry as key => val.

Example

package main

import (
	"errors"
	"fmt"
	"os"

	"github.com/psyb0t/glogger"
)

func main() {
	f, err := os.OpenFile("app.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		panic(fmt.Sprintf("error opening file: %v", err))
	}
	defer f.Close()

	// Set the output writer where log messages will be written.
	glogger.SetOutput(f)

	// Set the log format to JSON.
	glogger.SetLogFormat(glogger.LogFormatJSON)

	// Set the log level to "Info" or higher.
	glogger.SetLogLevel(glogger.LogLevelInfo)

	// Create a logger instance.
	logger := glogger.New(glogger.Caller{Service: "my-service", Package: "main", Function: "main"})

	// Log messages at different severity levels with various extra data.
	logger.RequestID("abc123").Debug("hello world")
	logger.TraceID("zzzsss").Info("hello world")
	logger.TraceID("zzzsss").SpanID("123456").Warn("hello world")
	logger.Err(errors.New("something bad happened")).Error("hello world")
	logger.Data("key", "value").Fatal("hello world")
}

The log file app.log will contain the following messages:

{"service":"my-service","package":"main","function":"main","time":"2023-01-04T00:50:04+02:00","level":"INFO","message":"hello world","requestID":"abc123","traceID":"zzzsss"}
{"service":"my-service","package":"main","function":"main","time":"2023-01-04T00:50:04+02:00","level":"WARN","message":"hello world","traceID":"zzzsss","spanID":"123456"}
{"service":"my-service","package":"main","function":"main","time":"2023-01-04T00:50:04+02:00","level":"ERROR","message":"hello world","error":"something bad happened"}
{"service":"my-service","package":"main","function":"main","time":"2023-01-04T00:50:04+02:00","level":"FATAL","message":"hello world","data":{"key":"value"}}

Documentation

Overview

Package glogger provides a logger that can be used to write log messages to an output destination, such as the standard output or a file. It has various levels of log severity (e.g. debug, info, warning, error, fatal) and the following formats: inline-string, json.

The global variables output, logFormat, and logLevel control the output destination, format, and minimum severity level of log messages, respectively. You can set these variables using the functions SetOutput, SetLogFormat, and SetLogLevel.

The function _print writes the given message to the output destination. The functions Debug, Info, Warn, Error, and Fatal write log messages with the corresponding severity levels, if the log level of the message is equal to or higher than the minimum log level set.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLogFormat

func SetLogFormat(format LogFormat)

SetLogFormat sets the global variable that controls the format of the log lines being printed

func SetLogLevel

func SetLogLevel(level LogLevel)

SetLogLevel sets the global variable that controls the minimum severity level required for a message to pass through. If an invalid value is passed it sets it to LogLevelDebug

func SetOutput

func SetOutput(o io.Writer)

SetOutput sets the global variable that specifies the output writer where log messages will be written.

For example, the following code configures the logger to write messages to a file named "app.log":

f, err := os.OpenFile("app.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
    log.Fatalf("error opening file: %v", err)
}
defer f.Close()

glogger.SetOutput(f)

Types

type Caller

type Caller struct {
	Service  string `json:"service,omitempty"`
	Package  string `json:"package,omitempty"`
	Receiver string `json:"receiver,omitempty"`
	Function string `json:"function,omitempty"`
}

Caller is the source of the logs. It is included in the log data

func (Caller) String

func (t Caller) String() string

String returns the string representation of a Caller

type LogFormat

type LogFormat uint8

LogFormat represents the format of a log message

const (
	LogFormatInlineString LogFormat = iota // (default)
	LogFormatJSON
)

Constants representing the available log formats.

func StrToLogFormat

func StrToLogFormat(str string) LogFormat

StrToLogFormat matches the given string to a supported LogFormat. If no matches are found it returns LogFormatInlineString

type LogLevel

type LogLevel uint8

LogLevel is the type that represents the log level

const (
	LogLevelDebug LogLevel = iota
	LogLevelInfo
	LogLevelWarn
	LogLevelError
	LogLevelFatal
)

Constants representing the available log levels.

func StrToLogLevel

func StrToLogLevel(str string) LogLevel

StrToLogLevel matches the given string to a supported LogLevel. If no matches are found it returns LogLevelDebug

type Logger

type Logger interface {
	// Err sets the error field in the log entry to the given value.
	Err(err error) Logger
	// RequestID sets the requestID field in the log entry to the given value.
	RequestID(id string) Logger
	// TraceID sets the traceID field in the log entry to the given value.
	TraceID(id string) Logger
	// SpanID sets the spanID field in the log entry to the given value.
	SpanID(id string) Logger
	// Data adds a data field in the log entry as key => val.
	Data(key string, val interface{}) Logger

	// Debug logs a message at the debug severity level.
	Debug(msg string)
	// Info logs a message at the info severity level.
	Info(msg string)
	// Warn logs a message at the warning severity level.
	Warn(msg string)
	// Error logs a message at the error severity level.
	Error(msg string)
	// Fatal logs a message at the fatal severity level and terminates the program.
	Fatal(msg string)
}

Logger is the interface that defines methods for logging messages at different severity levels.

func New

func New(caller Caller) Logger

New creates and returns a new Logger with the given caller.

Jump to

Keyboard shortcuts

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