golog

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: MIT Imports: 11 Imported by: 0

README

🔪 Golog

Go Report Card GoDoc License

This library is used to standardized log message for Runsystem service. You can use this library to simplify your service logging setup.

Installation

You can run this in your terminal

go get -u github.com/runsystemid/golog

Import this library in your main function or bootstrap function.

import "github.com/runsystemid/golog"

Usage

Config

There some required configs. Make sure to initialize the values first. Best practice is to put the values in config files to make it easier to update.

Config Description
App Application name
AppVer Application version
Env Environment (development or production)
FileLocation Location where the system log will be saved
FileTDRLocation Location where the tdr log will be saved
FileMaxSize Maximum size in Megabytes of a single log file. If the capacity reach, file will be saved but it will be renamed with suffix the current date
FileMaxBackup Maximum number of backup file that will not be deleted
FileMaxAge Number of days where the backup log will not be deleted
Stdout Log will be printed in console if the value is true
Loader

Initialize the loader by using

loggerConfig := golog.Config{
    App:             yourConfig.AppName,
    AppVer:          yourConfig.AppVersion,
    Env:             yourConfig.Environment,
    FileLocation:    yourConfig.Logger.FileLocation,
    FileTDRLocation: yourConfig.Logger.FileTDRLocation,
    FileMaxSize:     yourConfig.FileMaxSize
    FileMaxBackup:   yourConfig.FileMaxBackup
    FileMaxAge:      yourConfig.FileMaxAge,
    Stdout:          yourConfig.Stdout,
}

golog.Loader(loggerConfig)

Now you can use golog from anywhere in your project.

Example:

  • golog.Info(yourcontext, message) to print the log.

  • golog.Error(yourcontext, message, err) to print error log.

other interface golog.Debug, golog.Warn, golog.Fatal, golog.Panic also available.

Make sure to add value to these keys traceId, srcIP, port, path in your context to make it easier to trace.

Print Log

This library provide 2 kind of logs. System log and TDR log. Besides TDR function, it will print to system log.

Output

File and Stdout (configurable)

In default, the file will be printed in file. You can decide whether the output will be printed in console or not. Just put true in Stdout config attribute to print the log in console.

Rotate

Log file will be auto rotated based on file size. If the file size bigger than the config, will be saved to a new file with additional date in file name.

Contributing

Contributions are welcome! Please follow the Contribution Guidelines.

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SENSITIVE_ATTR = map[string]bool{
	"password":                 true,
	"license":                  true,
	"license_code":             true,
	"token":                    true,
	"access_token":             true,
	"refresh_token":            true,
	"bank_ac_no":               true,
	"id_number":                true,
	"mobile":                   true,
	"npwp":                     true,
	"phone":                    true,
	"card_no":                  true,
	"basic_salary":             true,
	"brutto":                   true,
	"employment_deduction":     true,
	"functional_allowance":     true,
	"health_allowance":         true,
	"health_deduction":         true,
	"incentive_income_tax_21":  true,
	"income_tax_21":            true,
	"jht_allowance":            true,
	"jkk_allowance":            true,
	"jkm_allowance":            true,
	"jkn_allowance":            true,
	"loan":                     true,
	"other_deduction":          true,
	"position_allowance":       true,
	"skill_allowance":          true,
	"special_region_allowance": true,
	"take_home_pay":            true,
	"total_allowance":          true,
	"total_deduction":          true,
	"total_wages":              true,
}
View Source
var SENSITIVE_HEADER = []string{
	"Authorization",
	"Signature",
	"Apikey",
}

Functions

func Debug

func Debug(ctx context.Context, msg string, fields ...zap.Field)

Debug logs a message at DebugLevel.

func Error

func Error(ctx context.Context, msg string, err error, fields ...zap.Field)

Error logs a message at ErrorLevel.

func Fatal

func Fatal(ctx context.Context, msg string, err error, fields ...zap.Field)

Fatal logs a message at FatalLevel.

The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.

func Info

func Info(ctx context.Context, msg string, fields ...zap.Field)

Info logs a message at InfoLevel.

func NewArrayStringField added in v1.0.5

func NewArrayStringField(key string, value []string) zap.Field

func NewBooleanField added in v1.0.5

func NewBooleanField(key string, value bool) zap.Field

func NewInt64Field added in v1.0.5

func NewInt64Field(key string, value int64) zap.Field

func NewIntField added in v1.0.5

func NewIntField(key string, value int) zap.Field

func NewObjectField added in v1.0.5

func NewObjectField(key string, value interface{}) zap.Field

func NewStringField added in v1.0.5

func NewStringField(key string, value string) zap.Field

func Panic

func Panic(ctx context.Context, msg string, err error, fields ...zap.Field)

Panic logs a message at PanicLevel.

The logger then panics, even if logging at PanicLevel is disabled.

func TDR

func TDR(ctx context.Context, model LogModel)

TDR (Transaction Detail Request) consist of request and response log

func Warn

func Warn(ctx context.Context, msg string, fields ...zap.Field)

Warn logs a message at WarnLevel.

Types

type Config

type Config struct {
	// App name
	App string `json:"app"`

	// App Version
	AppVer string `json:"appVer"`

	// Log environment (development or production)
	Env string `json:"env"`

	// Location where the system log will be saved
	FileLocation string `json:"fileLocation"`

	// Location where the tdr log will be saved
	FileTDRLocation string `json:"fileTDRLocation"`

	// Maximum size of a single log file.
	// If the capacity reach, file will be saved but it will be renamed
	// with suffix the current date
	FileMaxSize int `json:"fileMaxSize"`

	// Maximum number of backup file that will not be deleted
	FileMaxBackup int `json:"fileMaxBackup"`

	// Number of days where the backup log will not be deleted
	FileMaxAge int `json:"fileMaxAge"`

	// Log will be printed in console if the value is true
	Stdout bool `json:"stdout"`
}

type Log

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

func (*Log) Debug

func (l *Log) Debug(ctx context.Context, msg string, fields ...zap.Field)

func (*Log) Error

func (l *Log) Error(ctx context.Context, msg string, err error, fields ...zap.Field)

func (*Log) Fatal

func (l *Log) Fatal(ctx context.Context, msg string, err error, fields ...zap.Field)

func (*Log) Info

func (l *Log) Info(ctx context.Context, msg string, fields ...zap.Field)

func (*Log) Panic

func (l *Log) Panic(ctx context.Context, msg string, err error, fields ...zap.Field)

func (*Log) TDR

func (l *Log) TDR(ctx context.Context, log LogModel)

func (*Log) Warn

func (l *Log) Warn(ctx context.Context, msg string, fields ...zap.Field)

type LogModel

type LogModel struct {
	TraceID       string        `json:"traceId"`
	CorrelationID string        `json:"correlationId"`
	SrcIP         string        `json:"srcIp"`
	IP            string        `json:"ip"`
	Port          string        `json:"port"`
	Path          string        `json:"path"`
	Header        interface{}   `json:"header"`
	Request       interface{}   `json:"request"`
	StatusCode    string        `json:"statusCode"`
	HttpStatus    uint64        `json:"httpStatus"`
	Response      interface{}   `json:"response"`
	ResponseTime  time.Duration `json:"rt"`
	Error         interface{}   `json:"error"`
	OtherData     interface{}   `json:"otherData"`
}

type LoggerInterface

type LoggerInterface interface {
	Debug(ctx context.Context, message string, fields ...zap.Field)
	Info(ctx context.Context, message string, fields ...zap.Field)
	Warn(ctx context.Context, message string, fields ...zap.Field)
	Error(ctx context.Context, message string, err error, fields ...zap.Field)
	Fatal(ctx context.Context, message string, err error, fields ...zap.Field)
	Panic(ctx context.Context, message string, err error, fields ...zap.Field)
	TDR(ctx context.Context, tdr LogModel)
}

func Load

func Load(config Config) LoggerInterface

Loader to construct logger. So logger will

func NewLogger

func NewLogger(conf Config) LoggerInterface

Jump to

Keyboard shortcuts

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