logger

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: MIT Imports: 11 Imported by: 21

README

go-logger

Easy to use, extendable and superfast logging package for Go

Release Build Status Report codecov Go Sponsor Donate


Table of Contents


Installation

go-logger requires a supported release of Go.

go get -u github.com/mrz1836/go-logger

For use with Log Entries (Rapid7), change the environment variables:

export LOG_ENTRIES_TOKEN=your-token-here

(Optional) Set custom endpoint or port parameters

export LOG_ENTRIES_ENDPOINT=us.data.logs.insight.rapid7.com
export LOG_ENTRIES_PORT=514

Documentation

View the generated documentation

GoDoc

Features
  • Native logging package (extends log package)
  • Native support for Log Entries (Rapid7) with queueing
  • Test coverage on all custom methods
  • Supports different Rapid7 endpoints & ports
Library Deployment

goreleaser for easy binary or library deployment to GitHub and can be installed via: brew install goreleaser.

The .goreleaser.yml file is used to configure goreleaser.

Use make release-snap to create a snapshot version of the release, and finally make release to ship to production.

Makefile Commands

View all makefile commands

make help

List of all current commands:

all                  Runs multiple commands
clean                Remove previous builds and any test cache data
clean-mods           Remove all the Go mod cache
coverage             Shows the test coverage
godocs               Sync the latest tag with GoDocs
help                 Show this help message
install              Install the application
install-go           Install the application (Using Native Go)
lint                 Run the golangci-lint application (install if not found)
release              Full production release (creates release in Github)
release              Runs common.release then runs godocs
release-snap         Test the full release (build binaries)
release-test         Full production test release (everything except deploy)
replace-version      Replaces the version in HTML/JS (pre-deploy)
run-examples         Runs all the examples
tag                  Generate a new tag and push (tag version=0.0.0)
tag-remove           Remove a tag if found (tag-remove version=0.0.0)
tag-update           Update an existing tag to current commit (tag-update version=0.0.0)
test                 Runs vet, lint and ALL tests
test-ci              Runs all tests via CI (exports coverage)
test-ci-no-race      Runs all tests via CI (no race) (exports coverage)
test-ci-short        Runs unit tests via CI (exports coverage)
test-short           Runs vet, lint and tests (excludes integration tests)
uninstall            Uninstall the application (and remove files)
update-linter        Update the golangci-lint package (macOS only)
vet                  Run the Go vet application

Examples & Tests

All unit tests and examples run via GitHub Actions and uses Go version 1.17.x. View the configuration file.

Run all tests (including integration tests)

make test

Run tests (excluding integration tests)

make test-short

Benchmarks

Run the Go benchmarks:

make bench

Code Standards

Read more about this Go project's code standards.


Usage

Basic implementation:

package main

import "github.com/mrz1836/go-logger"

func main() {
	logger.Data(2, logger.DEBUG, "testing the go-logger package")
	// Output: type="debug" file="example/example.go" method="main.main" line="12" message="testing the go-logger package"
}

Maintainers

MrZ kayleg
MrZ kayleg

Contributing

View the contributing guidelines and please follow the code of conduct.

How can I help?

All kinds of contributions are welcome 🙌! The most basic way to show your support is to star 🌟 the project, or to raise issues 💬. You can also support this project by becoming a sponsor on GitHub 👏 or by making a bitcoin donation to ensure this journey continues indefinitely! 🚀


License

License

Documentation

Overview

Package logger is an easy-to-use, super-fast and extendable logging package for Go

Index

Examples

Constants

View Source
const (
	LogEntriesPort         = "10000"               // 80, 514, 443, 10000
	LogEntriesTestEndpoint = "34.253.67.177"       // This is an IP for now, since Github Actions fails on resolving the domains
	LogEntriesURL          = "data.logentries.com" // "data.insight.rapid7.com" "eu.data.logs.insight.rapid7.com"
	MaxRetryDelay          = 2 * time.Minute
	RetryDelay             = 100 * time.Millisecond
)

Package constants

View Source
const SlowQueryThreshold = 5 * time.Second

SlowQueryThreshold is the time cut-off for considering a query as "slow"

Variables

This section is empty.

Functions

func Data

func Data(stackLevel int, logLevel LogLevel, message string, args ...KeyValue)

Data will format the log message to a standardized log entries compatible format. stackLevel 2 will tag the log with the location from where Data is called. This will print using the implementation's Println function

func Errorfmt

func Errorfmt(stackLevel int, format string, v ...interface{})

Errorfmt is equivalent to Printf with a custom stack level, see Errorln for details

func Errorln

func Errorln(stackLevel int, v ...interface{})

Errorln is equivalent to Println() except the stack level can be set to generate the correct log tag. A stack level of 2 is will tag the log with the location from where Errorln is called, and is equivalent to Println. Larger numbers step further back in the stack

func Fatal added in v0.2.3

func Fatal(v ...interface{})

Fatal is equivalent to Print() followed by a call to os.Exit(1)

func Fatalf

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

Fatalf is equivalent to Printf() followed by a call to os.Exit(1)

func Fatalln

func Fatalln(v ...interface{})

Fatalln is equivalent to Println() followed by a call to os.Exit(1)

func FileTag

func FileTag(level int) string

FileTag tag file

Example

ExampleFileTag example using FileTag()

// fileTag := FileTag(1)
fileTag := "go-logger/logger_test.go:go-logger.ExampleFileTag:102"
fmt.Println(fileTag)
Output:

go-logger/logger_test.go:go-logger.ExampleFileTag:102

func FileTagComponents

func FileTagComponents(level int) []string

FileTagComponents file tag components

Example

ExampleFileTagComponents example using FileTagComponents()

fileTag := FileTagComponents(1)
fmt.Println(fileTag[0])
Output:

go-logger/logger_test.go

func NoFileData added in v0.1.3

func NoFileData(logLevel LogLevel, message string, args ...KeyValue)

NoFileData will format the log message to a standardized log entries compatible format. This will print using the implementation's Println function

func NoFilePrintf added in v0.1.3

func NoFilePrintf(format string, v ...interface{})

NoFilePrintf calls Output to print to the connected logger. Arguments are handled in the manner of fmt.Printf.

func NoFilePrintln added in v0.1.3

func NoFilePrintln(v ...interface{})

NoFilePrintln calls Output to print to the connected logger. Arguments are handled in the manner of fmt.Println.

func Panic added in v0.2.3

func Panic(v ...interface{})

Panic is equivalent to Print() followed by a call to os.Exit(1)

func Panicf added in v0.2.3

func Panicf(format string, v ...interface{})

Panicf is equivalent to Printf() followed by a call to os.Exit(1)

func Panicln added in v0.2.3

func Panicln(v ...interface{})

Panicln is equivalent to Println() followed by a call to os.Exit(1)

func Print added in v0.2.3

func Print(v ...interface{})

Print calls Output to print to the connected logger. Arguments are handled in the manner of fmt.Print.

func Printf

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

Printf calls Output to print to the connected logger. Arguments are handled in the manner of fmt.Printf.

func Println

func Println(v ...interface{})

Println calls Output to print to the connected logger. Arguments are handled in the manner of fmt.Println.

func SetImplementation

func SetImplementation(impl Logger)

SetImplementation allows the log implementation to be swapped at runtime

Types

type GormLogLevel added in v0.3.0

type GormLogLevel int

GormLogLevel is the GORM log level

const (
	// Silent silent log level
	Silent GormLogLevel = iota + 1

	// Error error log level
	Error

	// Warn warn log level
	Warn

	// Info info log level
	Info
)

type GormLoggerInterface added in v0.3.0

type GormLoggerInterface interface {
	Error(context.Context, string, ...interface{})
	GetMode() GormLogLevel
	GetStackLevel() int
	Info(context.Context, string, ...interface{})
	SetMode(GormLogLevel) GormLoggerInterface
	SetStackLevel(level int)
	Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)
	Warn(context.Context, string, ...interface{})
}

GormLoggerInterface is a logger interface to help work with GORM

func NewGormLogger added in v0.3.0

func NewGormLogger(debugging bool, stackLevel int) GormLoggerInterface

NewGormLogger will return a basic logger interface

type KeyValue

type KeyValue interface {
	Key() string
	Value() interface{}
}

KeyValue key value for errors

type LogClient added in v0.1.4

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

LogClient configuration

func NewLogEntriesClient

func NewLogEntriesClient(token, endpoint, port string) (*LogClient, error)

NewLogEntriesClient new client

func (*LogClient) Connect added in v0.1.4

func (l *LogClient) Connect() error

Connect will connect to Log Entries

func (*LogClient) Fatal added in v0.2.3

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

Fatal overloads built-in method

func (*LogClient) Fatalf added in v0.1.4

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

Fatalf overloads built-in method

func (*LogClient) Fatalln added in v0.1.4

func (l *LogClient) Fatalln(v ...interface{})

Fatalln overloads built-in method

func (*LogClient) Panic added in v0.2.3

func (l *LogClient) Panic(v ...interface{})

Panic overloads built-in method

func (*LogClient) Panicf added in v0.2.3

func (l *LogClient) Panicf(format string, v ...interface{})

Panicf overloads built-in method

func (*LogClient) Panicln added in v0.2.3

func (l *LogClient) Panicln(v ...interface{})

Panicln overloads built-in method

func (*LogClient) Print added in v0.2.3

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

Print overloads built-in method

func (*LogClient) Printf added in v0.1.4

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

Printf overloads built-in method

func (*LogClient) Println added in v0.1.4

func (l *LogClient) Println(v ...interface{})

Println overloads built-in method

func (*LogClient) ProcessQueue added in v0.1.4

func (l *LogClient) ProcessQueue()

ProcessQueue process the queue

type LogLevel

type LogLevel uint8

LogLevel our log level

const (
	DEBUG LogLevel = iota
	INFO
	WARN
	ERROR
)

Global constants

func (LogLevel) String

func (l LogLevel) String() string

String turn log level to string

Example

ExampleLogLevel_String example using level.String()

var level LogLevel
fmt.Println(level.String())
Output:

debug

type Logger

type Logger interface {
	Fatal(...interface{})
	Fatalf(format string, v ...interface{})
	Fatalln(v ...interface{})
	Panic(...interface{})
	Panicf(string, ...interface{})
	Panicln(...interface{})
	Print(...interface{})
	Printf(format string, v ...interface{})
	Println(v ...interface{})
}

Logger interface describes the functionality that a log service must implement

func GetImplementation added in v0.2.3

func GetImplementation() Logger

GetImplementation gets the current logger implementation

type Parameter added in v0.0.2

type Parameter struct {
	K string      `json:"key"`
	V interface{} `json:"value"`
}

Parameter is a standardized parameter struct for logger.Data()

func MakeParameter added in v0.0.2

func MakeParameter(key string, value interface{}) *Parameter

MakeParameter creates a new Parameter (key/value)

func (*Parameter) Key added in v0.0.2

func (p *Parameter) Key() string

Key implements the Logger KeyValue interface

func (*Parameter) String added in v0.0.2

func (p *Parameter) String() string

Parameter json encodes the parameter into standard key=>value JSON

func (*Parameter) Value added in v0.0.2

func (p *Parameter) Value() interface{}

Value implements the Logger KeyValue interface

Directories

Path Synopsis
Package main is an example package to show the use case of go-logger
Package main is an example package to show the use case of go-logger

Jump to

Keyboard shortcuts

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