Categorygithub.com/acsl-go/logger
modulepackage
0.0.4
Repository: https://github.com/acsl-go/logger.git
Documentation: pkg.go.dev

# README

acsl-go.logger

Description

acsl-go.logger is a logger module that can be used to log messages to the console or an Elasticsearch instance.

Installation

go get lab.afx.pub/ns/logger

Usage

The basic logger function is:

logger.Log(level int, format string, v ...interface{})

The level is used to determine what level the message is logged at. it could be one of the following values:

CONSTANT NAMEINTEGER VALUEDESCRIPTION
logger.FATAL0Fatal error
logger.ERROR1Error
logger.WARN2Warning
logger.INFO3Information
logger.DEBUG4Debugging message

You can also use the logger.Level variable to specify the maximum level of messages to log. For example:

logger.Level = logger.FATAL     // Only log messages with level FATAL
logger.Level = logger.INFO      // Only log messages with level INFO or higher
logger.Level = logger.DEBUG     // Log all messages

You can use the logger.Log directly, or use the following functions:

logger.Fatal(format string, v ...interface{})
logger.Error(format string, v ...interface{})
logger.Warn(format string, v ...interface{})
logger.Info(format string, v ...interface{})
logger.Debug(format string, v ...interface{})

The Extra Fields

The logger module will add some extra fields to the log message. The fields are:

FIELD NAMETYPEMETHODDESCRIPTION
Timestampint64/stringStdout,ElasticThe timestamp of the message.
PIDintStdout,ElasticThe process ID of the process that logged the message.
ProcessstringElasticThe name of the process that logged the message.
Levelint/stringStdout,ElasticThe level of the message.
IdentifierstringStdout,ElasticThe identifier of the message

The data type will be different when using different output methods. For example, when using the stdout output method, the Timestamp field will be a string, and when using the elastic output method, the Timestamp field will be an int64 that represents the number of milliseconds since the Unix epoch.

By default, the identifier field will be the combination of the process id and the process name. you can specify the identifier by using the logger.Identifier:

logger.Identifier = "my-identifier"

The Output Methods

Presently, we support the following output methods:

METHODFUNCTION NAMETARGET
stdoutlogger.LogStdoutStandard output
elasticlogger.LogElasticElasticsearch

By default, the stdout output method is used. You can change the output method by using the logger.LogMethod variable. The logger.LogMethod variable is an array of functions, and the index of the array is the level of the message. For example, if you want to use the elastic output method to log messages with level ERROR, you can use the following code:

logger.LogMethod[logger.ERROR] = logger.LogElastic

The usage of the elastic output method

The elastic output method will send the log message to an Elasticsearch instance. You MUST call logger.InitElastic function to initialize the elastic output method before specifying the logger.LogElastic to any element of the logger.LogMethod array.

Here is an example:

if err := InitElastic(&ElasticConfig{
    Addresses:     []string{"https://YOUR.ELASTIC:9200"},
    Username:      "elastic",
    Password:      "REPLACE WITH YOUR ELASTIC PASSWORD",
    CAFingerprint: "REPLACE WITH THE CA FINGERPRINT OF YOUR ELASTIC INSTANCE",
    Index:         "test",
}); err != nil {
    logger.LogMethod[logger.ERROR] = logger.LogStdout
} else {
    logger.LogMethod[logger.ERROR] = logger.LogElastic
}

The Configuration

The logger module can be initialized by using the logger.Init function. The logger.Init function accepts a logger.Config object as its parameter. The logger.Config object has the following fields:

type Config struct {
	Level      int    `mapstructure:"level" json:"level"`
	Identifier string `mapstructure:"identifier" json:"identifier"`
	Elastic    struct {
		Addresses     []string `mapstructure:"addresses" json:"addresses"`
		Username      string   `mapstructure:"username" json:"username"`
		Password      string   `mapstructure:"password" json:"password"`
		CAFingerprint string   `mapstructure:"ca" json:"ca"`
		Index         string   `mapstructure:"index" json:"index"`
	} `mapstructure:"elastic" json:"elastic"`
	Methods struct {
		LvFatal string `mapstructure:"fatal" json:"fatal"`
		LvError string `mapstructure:"error" json:"error"`
		LvWarn  string `mapstructure:"warn" json:"warn"`
		LvInfo  string `mapstructure:"info" json:"info"`
		LvDebug string `mapstructure:"debug" json:"debug"`
	} `mapstructure:"methods" json:"methods"`
}

The configuration can be loaded from a json file or as a part of viper configuration.

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author