astilog

package module
v1.10.1 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2022 License: MIT Imports: 17 Imported by: 215

README

GoReportCard GoDoc Travis Coveralls

astilog goal is to provide an implementation of astilog.CompleteLogger interface while restricting dependencies to astilog only.

It doesn't provide a global logger anymore since it should be a project-based decision and libs need an instance of astilog.CompleteLogger provided to them anyway.

It doesn't use logrus anymore since most of its features were not used. As a result astilog is now much simpler and quicker since it only implements a subset of logrus features.

Installation

Run the following command:

$ go get github.com/asticode/go-astilog

Usage

Create a logger using explicit configuration

// Create logger
l := astilog.New(astilog.Configuration{
    AppName: "myapp",
    Format: astilog.FormatText,
    Level: astilog.LevelWarn,
    Out: astilog.OutStderr,
    Source: true,
})

// Make sure to close the logger properly
defer l.Close()

Create a logger using flags only

// Parse flags
flag.Parse()

// Create logger
l := astilog.NewFromFlags()

// Make sure to close the logger properly
defer l.Close()

Log stuff

l.Debug("this is a log message")
l.Infof("this is a %s message", "log")

Log stuff using the context

// Add field to the context
ctx = astilog.ContextWithField(ctx, "key", "value")

// Log with context
l.DebugC(ctx, "this is a log message")
l.InfoCf(ctx, "this is a %s message", "log")

Add fields

l.WithFields(map[string]interface{}{
    "k1": "v1",
    "k2": "v2",
})

Outputs

Log to file

Set the Filename option to your file path.

Log to syslog

Set the Out option to syslog or astilog.OutSyslog if you're setting it in GO.

Log to stderr

Set the Out option to stderr or astilog.OutStderr if you're setting it in GO.

Log to stdout

Set the Out option to stdout or astilog.OutStdout if you're setting it in GO.

Formats

Text

Set the Format option to text or astilog.FormatText.

JSON

Set the Format option to json or astilog.FormatJSON.

Extra options

App name

Set the AppName option with your app name to see it in the logs.

Message key

Set the MessageKey option with your key to update the json key for the message field. Default is msg.

Source

Set the Source option to true to see the file:line that called the log function.

Timestamp format

Set the TimestampFormat option with your time format. If left empty, the duration since the beginning of the run will be logged.

Documentation

Index

Constants

View Source
const (
	FormatJSON       = "json"
	FormatMinimalist = "minimalist"
	FormatText       = "text"
)

Formats

View Source
const (
	OutStderr = "stderr"
	OutStdout = "stdout"
	OutSyslog = "syslog"
)

Outs

Variables

View Source
var (
	AppName         = flag.String("logger-app-name", "", "the logger app name")
	Filename        = flag.String("logger-filename", "", "the logger filename")
	Format          = flag.String("logger-format", "", "the logger format")
	Level           = flag.String("logger-level", "", "the logger level")
	MaxWriteLength  = flag.Int("logger-max-write-length", 0, "the logger max write length")
	MessageKey      = flag.String("logger-message-key", "", "the logger message key")
	Out             = flag.String("logger-out", "", "the logger out")
	Source          = flag.Bool("logger-source", false, "if true, then source is added to fields")
	TimestampFormat = flag.String("logger-timestamp-format", "", "the logger timestamp format")
	Verbose         = flag.Bool("v", false, "if true, then log level is debug")
)

Flags

Functions

func ContextWithField added in v1.1.0

func ContextWithField(ctx context.Context, k string, v interface{}) context.Context

func ContextWithFields added in v1.1.0

func ContextWithFields(ctx context.Context, fs map[string]interface{}) context.Context

func FieldsFromContext added in v1.8.0

func FieldsFromContext(ctx context.Context) (fs map[string]interface{})

Types

type Configuration

type Configuration struct {
	AppName         string              `toml:"app_name"`
	Filename        string              `toml:"filename"`
	Format          string              `toml:"format"`
	Level           astikit.LoggerLevel `toml:"level"`
	MaxWriteLength  int                 `toml:"max_write_length"`
	MessageKey      string              `toml:"message_key"`
	Out             string              `toml:"out"`
	Source          bool                `toml:"source"`
	TimestampFormat string              `toml:"timestamp_format"`
}

Configuration represents the configuration of the logger

func FlagConfig

func FlagConfig() (c Configuration)

FlagConfig generates a Configuration based on flags

type Logger

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

Logger represents an object that can log stuff

func New

func New(c Configuration) (l *Logger)

New creates a new Logger

func NewFromFlags added in v1.5.0

func NewFromFlags() (l *Logger)

NewFromFlags creates a new Logger based on flags

func (*Logger) Close added in v1.5.0

func (l *Logger) Close() error

Close closes the logger properly

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

func (*Logger) DebugC added in v1.1.0

func (l *Logger) DebugC(ctx context.Context, v ...interface{})

func (*Logger) DebugCf added in v1.1.0

func (l *Logger) DebugCf(ctx context.Context, format string, v ...interface{})

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

func (*Logger) ErrorC added in v1.1.0

func (l *Logger) ErrorC(ctx context.Context, v ...interface{})

func (*Logger) ErrorCf added in v1.1.0

func (l *Logger) ErrorCf(ctx context.Context, format string, v ...interface{})

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

func (*Logger) FatalC added in v1.1.0

func (l *Logger) FatalC(ctx context.Context, v ...interface{})

func (*Logger) FatalCf added in v1.1.0

func (l *Logger) FatalCf(ctx context.Context, format string, v ...interface{})

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, v ...interface{})

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

func (*Logger) InfoC added in v1.1.0

func (l *Logger) InfoC(ctx context.Context, v ...interface{})

func (*Logger) InfoCf added in v1.1.0

func (l *Logger) InfoCf(ctx context.Context, format string, v ...interface{})

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

func (*Logger) Print added in v1.4.0

func (l *Logger) Print(v ...interface{})

func (*Logger) Printf added in v1.4.0

func (l *Logger) Printf(format string, v ...interface{})

func (*Logger) Warn

func (l *Logger) Warn(v ...interface{})

func (*Logger) WarnC added in v1.1.0

func (l *Logger) WarnC(ctx context.Context, v ...interface{})

func (*Logger) WarnCf added in v1.1.0

func (l *Logger) WarnCf(ctx context.Context, format string, v ...interface{})

func (*Logger) Warnf

func (l *Logger) Warnf(format string, v ...interface{})

func (*Logger) WithField added in v1.1.0

func (l *Logger) WithField(k string, v interface{})

WithField adds a field to the logger

func (*Logger) WithFields added in v1.1.0

func (l *Logger) WithFields(fs map[string]interface{})

WithFields adds fields to the logger

func (*Logger) Write added in v1.10.0

func (l *Logger) Write(lv astikit.LoggerLevel, v ...interface{})

func (*Logger) WriteC added in v1.10.0

func (l *Logger) WriteC(ctx context.Context, lv astikit.LoggerLevel, v ...interface{})

func (*Logger) WriteCf added in v1.10.0

func (l *Logger) WriteCf(ctx context.Context, lv astikit.LoggerLevel, format string, v ...interface{})

func (*Logger) Writef added in v1.10.0

func (l *Logger) Writef(lv astikit.LoggerLevel, format string, v ...interface{})

Jump to

Keyboard shortcuts

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