logger

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: BSD-3-Clause Imports: 6 Imported by: 0

README

logger

This package provides logging utilities with different log levels and the ability to log to multiple loggers (e.g., files, remote systems). It includes the FileLogger for logging to a file and MultiLoggers for logging to multiple loggers simultaneously.

Static Badge

GoDoc GitHub stars Go Report Card codecov

Versions

Stable Version GitHub Release GitHub Release

Installation

To use this package in your Go project, run:

go get -u github.com/gouef/logger

FileLogger

The FileLogger logs messages to a file. You can specify the log levels you want to enable, and it will write the log entries in a JSON-like format. It's implements github.com/gouef/standards/Logger.

Usage
package main

import (
	"github.com/gouef/logger"
	"github.com/gouef/standards"
	"log"
)

func main() {
	// Create a new FileLogger
	fileLogger, err := logger.NewFileLogger("app.log", standards.INFO, standards.ERROR)
	if err != nil {
		log.Fatal(err)
	}
	defer fileLogger.Close()

	// Log an info message
	err = fileLogger.Info("This is an info message", nil)
	if err != nil {
		log.Fatal(err)
	}

	// Log an error message
	err = fileLogger.Error("This is an error message", nil)
	if err != nil {
		log.Fatal(err)
	}
}

Methods
  • NewFileLogger(filePath string, levels ...standards.LogLevel) (*FileLogger, error) Creates a new instance of FileLogger. If no log levels are specified, it defaults to log levels: EMERGENCY, CRITICAL, ERROR, ALERT, and WARNING.
  • Log(level standards.LogLevel, message string, context []any) error Logs a message with the specified level. Context is an optional array of additional data.
  • Close() error Closes the underlying log file.

The following log level methods are provided for convenience:

  • Emergency(message string, context []any) error
  • Alert(message string, context []any) error
  • Critical(message string, context []any) error
  • Error(message string, context []any) error
  • Warning(message string, context []any) error
  • Notice(message string, context []any) error
  • Info(message string, context []any) error
  • Debug(message string, context []any) error

MultiLogger

The MultiLogger allows you to log to multiple loggers at once (e.g., file, console, remote logging system). It's implements github.com/gouef/standards/Logger.

Usage
package main

import (
	"github.com/gouef/logger"
	"github.com/gouef/standards"
	"log"
)

func main() {
	// Create multiple loggers (e.g., FileLogger, ConsoleLogger)
	fileLogger, err := logger.NewFileLogger("app.log", standards.INFO, standards.ERROR)
	if err != nil {
		log.Fatal(err)
	}

	// Combine the loggers into MultiLoggers
	multiLogger := logger.NewMultiLogger(fileLogger)

	// Log a message to all loggers
	err = multiLogger.Info("This message will be logged to all loggers", nil)
	if err != nil {
		log.Fatal(err)
	}
}

Contributing

Read Contributing

Contributors

JanGalek actions-user

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileLogger

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

func NewFileLogger

func NewFileLogger(filePath string, levels ...standards.LogLevel) (*FileLogger, error)

NewFileLogger creates a new FileLogger instance.

func (*FileLogger) Alert

func (l *FileLogger) Alert(message string, context []any) error

Alert Action must be taken immediately.

func (*FileLogger) Close

func (l *FileLogger) Close() error

Close closes the underlying log file.

func (*FileLogger) Critical

func (l *FileLogger) Critical(message string, context []any) error

Critical Critical conditions.

func (*FileLogger) Debug

func (l *FileLogger) Debug(message string, context []any) error

Debug Detailed debug information.

func (*FileLogger) Emergency

func (l *FileLogger) Emergency(message string, context []any) error

Emergency System is unusable

func (*FileLogger) Error

func (l *FileLogger) Error(message string, context []any) error

Error Runtime errors that do not require immediate action but should typically

func (*FileLogger) Info

func (l *FileLogger) Info(message string, context []any) error

Info Interesting events

func (*FileLogger) Log

func (l *FileLogger) Log(level standards.LogLevel, message string, context []any) error

Log writes a log entry with the specified level, message, and optional context.

func (*FileLogger) Notice

func (l *FileLogger) Notice(message string, context []any) error

Notice Normal but significant events.

func (*FileLogger) Warning

func (l *FileLogger) Warning(message string, context []any) error

Warning Exceptional occurrences that are not errors.

type MultiLogger

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

func NewMultiLogger

func NewMultiLogger(loggers ...standards.Logger) *MultiLogger

func (*MultiLogger) Alert

func (l *MultiLogger) Alert(message string, context []any) error

Alert Action must be taken immediately.

func (*MultiLogger) Critical

func (l *MultiLogger) Critical(message string, context []any) error

Critical Critical conditions.

func (*MultiLogger) Debug

func (l *MultiLogger) Debug(message string, context []any) error

Debug Detailed debug information.

func (*MultiLogger) Emergency

func (l *MultiLogger) Emergency(message string, context []any) error

Emergency System is unusable

func (*MultiLogger) Error

func (l *MultiLogger) Error(message string, context []any) error

Error Runtime errors that do not require immediate action but should typically

func (*MultiLogger) Info

func (l *MultiLogger) Info(message string, context []any) error

Info Interesting events

func (*MultiLogger) Log

func (l *MultiLogger) Log(level standards.LogLevel, message string, context []any) error

Log Logs with an arbitrary level.

func (*MultiLogger) Notice

func (l *MultiLogger) Notice(message string, context []any) error

Notice Normal but significant events.

func (*MultiLogger) Warning

func (l *MultiLogger) Warning(message string, context []any) error

Warning Exceptional occurrences that are not errors.

Jump to

Keyboard shortcuts

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