logger

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2023 License: MIT Imports: 9 Imported by: 0

README

logger

This package provides a convenient tool for logging Go programs. It includes an already configured FastLogger that offers fast logging performance, with each operation taking less than 1 microsecond.

Installation

To install the logger package, use the following command: go get github.com/Eugene-Usachev/logger

Usage

Here's an example of how to use the FastLogger:

package main

import (
	"errors"
	"github.com/Eugene-Usachev/fastbytes"
	testLogger "github.com/Eugene-Usachev/logger"
	"os"
	"path/filepath"
	"time"
)

func main() {

	logsDir := filepath.Join("../", "logs")
	err := os.Mkdir(logsDir, 0777)
	if err != nil && !errors.Is(err, os.ErrExist) {
		return
	}

	filePathInfo := filepath.Join(logsDir, "info.txt")
	infoFile, _ := os.OpenFile(filePathInfo, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
	defer infoFile.Close()

	filePathError := filepath.Join(logsDir, "error.txt")
	errorFile, _ := os.OpenFile(filePathError, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
	defer errorFile.Close()

	filePathWarning := filepath.Join(logsDir, "warning.txt")
	warningFile, _ := os.OpenFile(filePathWarning, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
	defer warningFile.Close()

	filePathSuccess := filepath.Join(logsDir, "success.txt")
	successFile, _ := os.OpenFile(filePathSuccess, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
	defer successFile.Close()

	filePathRecord := filepath.Join(logsDir, "record.txt")
	recordFile, _ := os.OpenFile(filePathRecord, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
	defer recordFile.Close()

	filePathRaw := filepath.Join(logsDir, "raw.txt")
	rawFile, _ := os.OpenFile(filePathRaw, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
	defer rawFile.Close()

	filePathFatal := filepath.Join(logsDir, "fatal.txt")
	fatalFile, _ := os.OpenFile(filePathFatal, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
	defer fatalFile.Close()

	cfg := &testLogger.StandardLoggerConfig{
		IsWritingToTheConsole: true,
		ErrorWriter:           errorFile,
		WarningWriter:         warningFile,
		InfoWriter:            infoFile,
		SuccessWriter:         successFile,
		FatalWriter:           fatalFile,
		RecordWriter:          recordFile,
		RawWriter:             rawFile,
		ShowDate:              true,
	}
	fastLogger := testLogger.NewFastLogger(&testLogger.FastLoggerConfig{
		StandardLoggerConfig: *cfg,
		FlushInterval:        1 * time.Second,
		FatalFunc:            nil,
	})

	fastLogger.Info("[info fast] Hello, World!")
	fastLogger.Error("[error fast] Error message")
	fastLogger.Warning("[warning fast] Warning message")
	fastLogger.Success("[success fast] Success message")
	fastLogger.Record(testLogger.Builder().ShowDate().Prefix("[record fast] ").NewLine(true).AppendArgs("Build message").Build())
	fastLogger.Raw(fastbytes.S2B("[raw fast] Raw message!\n"))

	prepared := testLogger.Builder().Prefix("[my prefix] ").NewLine(true).AppendArgs("Hello, World!").Prepare()
	fastLogger.InfoPrepare(prepared)

	fastLogger.Fatal("[fatal fast] Fatal message")
}

Contributing

We welcome contributions to the logger package! If you encounter any issues or have suggestions for improvements, please feel free to open an issue or contribute directly to the codebase. Your feedback and contributions are valuable in making this package even better.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Now = func() atomic.Value {
	var v atomic.Value
	v.Store(make([]byte, 0))
	return v
}()

Functions

This section is empty.

Types

type FastLogger

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

FastLogger is a logger that writes to the standard logger with buffers. FastLogger is thread-safe and faster than StandardLogger by many orders. FastLogger will not log buffers if the application crashes before the recording interval has passed! If you don't use panics outside FastLogger, FastLogger will only not record if the machine is turned off. Use FastLogger.Fatal() or FastLogger.FormatFatal() instead of panic, or use FastLogger.Flush() before shutting down the application.

Example:

logger := NewFastLogger(&FastLoggerConfig{
	StandardLoggerConfig: StandardLoggerConfig{
		IsWritingToTheConsole: true,
		ErrorWriter:           errorFile,
		WarningWriter:         warningFile,
		InfoWriter:            infoFile,
		SuccessWriter:         successFile,
		FatalWriter:           fatalFile,
		RecordWriter:          recordFile,
		RawWriter:             rawFile,
	},
	FlushInterval: 1 * time.Second,
	FatalFunc:     func(reason any) {
	panic(fmt.Sprintf("logger could not write to the file reason: %v", reason))
	},
})

func NewFastLogger

func NewFastLogger(cfg *FastLoggerConfig) *FastLogger

NewFastLogger creates a new FastLogger.

func (*FastLogger) Error

func (logger *FastLogger) Error(args ...interface{})

Error logs a message to the logger.stdLogger.errorWriter.

func (*FastLogger) ErrorPrepare

func (logger *FastLogger) ErrorPrepare(record *Record)

ErrorPrepare logs a prepared record to the logger.errorWriter. Will not reset the record.

func (*FastLogger) Fatal

func (logger *FastLogger) Fatal(args ...interface{})

Fatal logs a message to the logger.stdLogger.fatalWriter.

func (*FastLogger) FatalPrepare

func (logger *FastLogger) FatalPrepare(record *Record)

FatalPrepare logs a prepared record to the logger.fatalWriter. Will not reset the record.

func (*FastLogger) Flush

func (logger *FastLogger) Flush()

Flush flushes all logs to the logger.

func (*FastLogger) FormatError

func (logger *FastLogger) FormatError(f string, args ...interface{})

FormatError logs a message with format to the logger.stdLogger.errorWriter.

func (*FastLogger) FormatFatal

func (logger *FastLogger) FormatFatal(f string, args ...interface{})

FormatFatal logs a message with format to the logger.stdLogger.fatalWriter.

func (*FastLogger) FormatInfo

func (logger *FastLogger) FormatInfo(f string, args ...interface{})

FormatInfo logs a message with format to the logger.stdLogger.infoWriter.

func (*FastLogger) FormatSuccess

func (logger *FastLogger) FormatSuccess(f string, args ...interface{})

FormatSuccess logs a message with format to the logger.stdLogger.successWriter.

func (*FastLogger) FormatWarning

func (logger *FastLogger) FormatWarning(f string, args ...interface{})

FormatWarning logs a message with format to the logger.stdLogger.warningWriter.

func (*FastLogger) Info

func (logger *FastLogger) Info(args ...interface{})

func (*FastLogger) InfoPrepare

func (logger *FastLogger) InfoPrepare(record *Record)

InfoPrepare logs a prepared record to the logger.infoWriter. Will not reset the record.

func (*FastLogger) Raw

func (logger *FastLogger) Raw(data []byte)

Raw logs a raw log to the logger.stdLogger.rawWriter.

func (*FastLogger) Record

func (logger *FastLogger) Record(record *Record)

Record logs a record to the logger.stdLogger.recordWriter.

func (*FastLogger) Stop

func (logger *FastLogger) Stop()

Stop stops the logger.

func (*FastLogger) StopWithoutFlush

func (logger *FastLogger) StopWithoutFlush()

StopWithoutFlush stops the logger without flushing. WILL CLEAR NOT FLUSHED LOGS!

func (*FastLogger) Success

func (logger *FastLogger) Success(args ...interface{})

Success logs a message to the logger.stdLogger.successWriter.

func (*FastLogger) SuccessPrepare

func (logger *FastLogger) SuccessPrepare(record *Record)

SuccessPrepare logs a prepared record to the logger.successWriter. Will not reset the record.

func (*FastLogger) Warning

func (logger *FastLogger) Warning(args ...interface{})

Warning logs a message to the logger.stdLogger.warningWriter.

func (*FastLogger) WarningPrepare

func (logger *FastLogger) WarningPrepare(record *Record)

WarningPrepare logs a prepared record to the logger.warningWriter. Will not reset the record.

type FastLoggerConfig

type FastLoggerConfig struct {
	StandardLoggerConfig

	// FlushInterval is the interval between flushes to the writers.
	FlushInterval time.Duration
	// FatalFunc is the function to call when a fatal error occurs in the logger.
	FatalFunc func(reason any)
}

type Record

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

func Builder

func Builder() *Record

func NewBuilder

func NewBuilder() *Record

func (*Record) AppendArgs

func (r *Record) AppendArgs(args ...interface{}) *Record

func (*Record) AppendFormat

func (r *Record) AppendFormat(format string, args ...interface{}) *Record

func (*Record) Build

func (r *Record) Build() *Record

func (*Record) NewLine

func (r *Record) NewLine(flag bool) *Record

func (*Record) NoDate

func (r *Record) NoDate() *Record

func (*Record) Prefix

func (r *Record) Prefix(prefix string) *Record

func (*Record) Prepare

func (r *Record) Prepare() *Record

Prepare a record to use with Prepare functions.

func (*Record) Reset

func (r *Record) Reset()

func (*Record) ShowDate

func (r *Record) ShowDate() *Record

type StandardLogger

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

func NewStandardLogger

func NewStandardLogger(cfg *StandardLoggerConfig) *StandardLogger

NewStandardLogger creates a new StandardLogger.

func (*StandardLogger) Error

func (logger *StandardLogger) Error(args ...interface{})

Error logs a message to the logger.errorWriter.

func (*StandardLogger) ErrorPrepare

func (logger *StandardLogger) ErrorPrepare(record *Record)

ErrorPrepare logs a prepared record to the logger.errorWriter. Will not reset the record.

func (*StandardLogger) Fatal

func (logger *StandardLogger) Fatal(args ...interface{})

Fatal logs a message to the logger.fatalWriter. It will exit the program.

func (*StandardLogger) FatalPrepare

func (logger *StandardLogger) FatalPrepare(record *Record)

FatalPrepare logs a prepared record to the logger.fatalWriter. Will not reset the record.

func (*StandardLogger) FormatError

func (logger *StandardLogger) FormatError(f string, args ...interface{})

FormatError logs a message with format to the logger.errorWriter.

func (*StandardLogger) FormatFatal

func (logger *StandardLogger) FormatFatal(f string, args ...interface{})

FormatFatal logs a message with format to the logger.fatalWriter. It will exit the program.

func (*StandardLogger) FormatInfo

func (logger *StandardLogger) FormatInfo(f string, args ...interface{})

FormatInfo logs a message with format to the logger.infoWriter.

func (*StandardLogger) FormatSuccess

func (logger *StandardLogger) FormatSuccess(f string, args ...interface{})

FormatSuccess logs a message with format to the logger.successWriter.

func (*StandardLogger) FormatWarning

func (logger *StandardLogger) FormatWarning(f string, args ...interface{})

FormatWarning logs a message with format to the logger.warningWriter.

func (*StandardLogger) Info

func (logger *StandardLogger) Info(args ...interface{})

Info logs a message to the logger.infoWriter.

func (*StandardLogger) InfoPrepare

func (logger *StandardLogger) InfoPrepare(record *Record)

InfoPrepare logs a prepared record to the logger.infoWriter. Will not reset the record.

func (*StandardLogger) Raw

func (logger *StandardLogger) Raw(record []byte)

Raw logs a raw log to the logger.rawWriter.

func (*StandardLogger) RawWithWriter

func (logger *StandardLogger) RawWithWriter(record []byte, writer io.Writer)

RawWithWriter logs a raw log to the writer.

func (*StandardLogger) Record

func (logger *StandardLogger) Record(record *Record)

Record logs a record to the logger.recordWriter. You can create a record with Builder(). Will reset the record.

func (*StandardLogger) RecordWithWriter

func (logger *StandardLogger) RecordWithWriter(record Record, writer io.Writer)

RecordWithWriter logs a record to the writer. You can create a record with Builder().

func (*StandardLogger) Success

func (logger *StandardLogger) Success(args ...interface{})

Success logs a message to the logger.successWriter.

func (*StandardLogger) SuccessPrepare

func (logger *StandardLogger) SuccessPrepare(record *Record)

SuccessPrepare logs a prepared record to the logger.successWriter. Will not reset the record.

func (*StandardLogger) Warning

func (logger *StandardLogger) Warning(args ...interface{})

Warning logs a message to the logger.warningWriter.

func (*StandardLogger) WarningPrepare

func (logger *StandardLogger) WarningPrepare(record *Record)

WarningPrepare logs a prepared record to the logger.warningWriter. Will not reset the record.

type StandardLoggerConfig

type StandardLoggerConfig struct {
	// IsWritingToTheConsole indicates whether the logger should write to the console.
	IsWritingToTheConsole bool
	// ErrorWriter is the writer to which errors will be written.
	ErrorWriter io.Writer
	// WarningWriter is the writer to which warnings will be written.
	WarningWriter io.Writer
	// InfoWriter is the writer to which information will be written.
	InfoWriter io.Writer
	// SuccessWriter is the writer to which successes will be written.
	SuccessWriter io.Writer
	// FatalWriter is the writer to which errors will be written.
	FatalWriter io.Writer
	// RecordWriter is the writer to which records will be written.
	RecordWriter io.Writer
	// RawWriter is the writer to which raw logs will be written.
	RawWriter io.Writer

	ShowDate bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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