logtor

package module
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: GPL-3.0 Imports: 5 Imported by: 0

README

logtor (Log Creator)

logtor is a package that allows you to save log records with different outputs. You can make changes to the running application. You can log your operations according to log levels.

There are three log recorders ready-made. You can use as many loggers as you want after defining the specified functions.

Installation

go get https://github.com/Eyup-Devop/logtor

Example Usage

package main

import (
	"github.com/Eyup-Devop/logtor"
	"github.com/Eyup-Devop/logtor/creators"
	"github.com/Eyup-Devop/logtor/types"
)

func main() {
	console, _ := creators.NewBaseCreator("Console", 3, 5)

	newLogtor := logtor.NewLogtor()
	newLogtor.AddLogCreators(console)
	newLogtor.SetLogLevel(types.TRACE)
	newLogtor.LogIt(types.INFO, "Example Info")
}

Documentation

Overview

Package logtor provides a flexible logging framework that allows the coordination of multiple log creators with different destinations and log levels. It includes a central manager, Logtor, for managing log creators and controlling the global log level.

Logtor allows you to log messages to various destinations simultaneously (e.g., file, console) and dynamically switch between different log creators. Each log creator must implement the LogCreator interface, providing methods for logging messages, retrieving the log creator's name, setting call depth, and performing cleanup operations during shutdown.

Usage: - Create a new Logtor instance with NewLogtor(). - Add log creators using AddLogCreators(), specifying destinations such as files or brokers. - Change the active log creator with ChangeLogCreator() to direct log messages to a specific log creator. - Set the global log level with SetLogLevel() to control which log messages are recorded. - Use LogIt() or LogItWithCallDepth() to log messages with the currently active log creator. - Gracefully shut down log creators using Shutdown().

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LogCreator

type LogCreator interface {
	// LogIt logs a message with the specified log level and returns true if successful.
	LogIt(level types.LogLevel, logMessage interface{}) bool

	// LogItWithCallDepth logs a message with the specified log level and call depth and returns true if successful.
	LogItWithCallDepth(level types.LogLevel, callDepth int, logMessage interface{}) bool

	// LogName returns the name of the log creators.
	LogName() types.LogCreatorName

	// SetCallDepth sets the call depth for the log creator.
	SetCallDepth(callDepth int)

	// CallDepth returns the call depth for the log creator.
	CallDepth() int

	// Shutdown performs any necessary cleanup or shutdown operations for the log creator.
	Shutdown()
}

LogCreator is an interface for log creator that handle log messages of different log levels.

A log creator must implement the following methods:

- LogIt: Logs a message with the specified log level. - LogName: Returns the name of the log creator. - Shutdown: Performs any necessary cleanup or shutdown operations for the log creator.

type Logtor

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

Logtor is a central logging manager that coordinates multiple log creators and log levels.

It manages a list of log creators, allowing you to log messages to different destinations (e.g., file, console) simultaneously. You can set the global log level for Logtor to control which log messages are recorded.

Fields:

  • logCreatorList: A map of LogCreatorName to LogCreator, representing registered log creator.
  • logLevel: The global log level that controls which log messages are created.
  • activeLogCreator: The currently active log creator for logging messages.
  • changeMutex: A read-write mutex to control concurrent access to Logtor's fields.

func NewLogtor

func NewLogtor() *Logtor

NewLogtor creates a new Logtor instance with default settings.

It initializes a Logtor with an empty list of log creators, a global log level set to NONE, and no active log creator selected.

Returns:

  • *Logtor: A pointer to the newly created Logtor.

func (*Logtor) AddLogCreators

func (l *Logtor) AddLogCreators(logCreators ...LogCreator)

AddLogcreators registers one or more log creators with the Logtor instance.

This method allows you to add multiple log creators to the Logtor. The log creators are identified by their names and can be used for logging messages. If no active log creator is currently set, the first added log creator becomes the active one.

Parameters:

  • logCreators: One or more LogCreator instances to be added to the Logtor.

func (*Logtor) ChangeActiveLogCreator

func (l *Logtor) ChangeActiveLogCreator(w http.ResponseWriter, r *http.Request)

func (*Logtor) ChangeLogCreator

func (l *Logtor) ChangeLogCreator(logCreatorName types.LogCreatorName) bool

ChangeLogCreator changes the active log creator to the one with the specified name.

Use this method to switch the active log creator to the one identified by the provided LogCreatorName. This allows you to direct log messages to a specific log creator from the list of registered log creators.

Parameters:

  • logCreatorName: The name of the log creator to make active.

Returns:

  • bool: True if the log creator with the specified name exists and is successfully set as active; false if the log creator does not exist.

func (*Logtor) GetActiveLogLevel

func (l *Logtor) GetActiveLogLevel(w http.ResponseWriter, r *http.Request)

func (*Logtor) GetCurrentLogCreator

func (l *Logtor) GetCurrentLogCreator(w http.ResponseWriter, r *http.Request)

func (*Logtor) GetLogCreatorList

func (l *Logtor) GetLogCreatorList(w http.ResponseWriter, r *http.Request)

func (*Logtor) GetLogLevelList

func (l *Logtor) GetLogLevelList(w http.ResponseWriter, r *http.Request)

func (*Logtor) LogCreator

func (l *Logtor) LogCreator() LogCreator

LogCreator returns the currently active log creator of the Logtor instance.

Use this method to obtain the currently active log creator, which is responsible for recording log messages at the global log level. The returned value is of type LogCreator.

Returns:

  • LogCreator: The currently active log creator.

func (*Logtor) LogIt

func (l *Logtor) LogIt(level types.LogLevel, logMessage interface{}) bool

LogIt logs a message at the specified log level using the currently active log creator.

This method allows you to log a message at a specific log level, subject to the global log level configured for the Logtor. If the provided log level is acceptable based on the global log level, the message is recorded by the currently active log creator.

Parameters:

  • level: The log level for the message (e.g., INFO, DEBUG).
  • logMessage: The message to be logged, which can be of any type.

Returns:

  • bool: True if the message was successfully logged; false if it was skipped due to the log level.

func (*Logtor) LogItWithCallDepth

func (l *Logtor) LogItWithCallDepth(level types.LogLevel, callDepth int, logMessage interface{}) bool

LogIt logs a message at the specified log level using the currently active log creator.

This method allows you to log a message at a specific log level, subject to the global log level configured for the Logtor. If the provided log level is acceptable based on the global log level, the message is recorded by the currently active log creator.

Parameters:

  • level: The log level for the message (e.g., INFO, DEBUG).
  • callDepth: The call depth for calling function.
  • logMessage: The message to be logged, which can be of any type.

Returns:

  • bool: True if the message was successfully logged; false if it was skipped due to the log level.

func (*Logtor) LogLevel

func (l *Logtor) LogLevel() types.LogLevel

LogLevel returns the current global log level of the Logtor instance.

Use this method to retrieve the current global log level, which determines which log messages are recorded and displayed. The returned value is of type LogLevelType.

Returns:

  • LogLevelType: The current global log level.

func (*Logtor) SetLogLevel

func (l *Logtor) SetLogLevel(logLevel types.LogLevel) bool

SetLogLevel sets the global log level for the Logtor instance.

You can use this method to change the log level for the Logtor, which controls which log messages are recorded and displayed. The log level should be one of the predefined LogLevelType constants.

Parameters:

  • logLevel: The new global log level to set for the Logtor.

func (*Logtor) SetLogLevelHandlerFunc

func (l *Logtor) SetLogLevelHandlerFunc(w http.ResponseWriter, r *http.Request)

func (*Logtor) Shutdown

func (l *Logtor) Shutdown()

Shutdown gracefully shuts down all registered log creators.

Use this method to perform any necessary cleanup or shutdown operations for all registered log creators. It iterates through the list of log creators and calls their respective shutdown methods.

Directories

Path Synopsis
Package logtor provides log creators and loggers for various destinations.
Package logtor provides log creators and loggers for various destinations.
Package logtor provides constants and utility functions related to logging, including LogLevel constants, LogCreatorName type, color codes for different log levels, and functions for color formatting and checking whether a log level is acceptable based on a selected level.
Package logtor provides constants and utility functions related to logging, including LogLevel constants, LogCreatorName type, color codes for different log levels, and functions for color formatting and checking whether a log level is acceptable based on a selected level.

Jump to

Keyboard shortcuts

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