log

package module
v1.3.7 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2024 License: GPL-3.0 Imports: 6 Imported by: 10

README

Log v1

Simple, fast, structured and level registration in Go.

Overview

This package offers several functions that allow you to trace controlled log errors in the files and functions where they occur, as well as useful programmer log comments in your code. This package will help you identify and segment the different types of errors log or unique circumstances during the execution of your program using criteria according to the level of impact on the business rules of its development. The package prints the useful information for the programmer in a human readable format.

Installation

go get -u https://github.com/jgolang/log

Quick Start

package main

import "github.com/jgolang/log"

func main(){
    log.Println("My info....")
}
Terminal output:
2020/07/05 13:32:03     INFO    /dir/file.go:10 (function) My info...

Mode

You can configure the package depending on your needs to display certain types of log by defining an environment variable on the system that runs your program.

[user@ /home]# export MODE="DEV"
Allowed modes
Mode Description
PROD Only prints error log
DEV Print all

Note: Error logs are printed regardless of this setting.


Released under the GPL-3.0.

Documentation

Overview

Package log provides simple, fast, structured and level registration in Go.

This package offers several functions that allow you to trace controlled log errors in the files and functions where they occur, as well as useful programmer log comments in your code. This package will help you identify and segment the different types of errors log or unique circumstances during the execution of your program using criteria according to the level of impact on the business rules of its development. The package prints the useful information for the programmer in a human readable format.

Installation

Run command in terminal:

go get -u https://github.com/jgolang/log

Quick Start

This is a simple example of how the package is implemented with a basic function.

package main

import "github.com/jgolang/log"

func main(){
    log.Println("My info....")
}

Terminal output:

2020/07/05 13:32:03     INFO    /dir/file.go:10 (function) My info...

Configuration Modes

You can configure the package depending on your needs to display certain types of log by defining an environment variable on the system that runs your program.

[user@ /home]# export MODE="DEV"

Allowed modes

PROD | DEV

Note: Error logs are printed regardless of this setting.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...any)

Debug logs a debug-level message using the global logger. msg: The message to log. args: Additional arguments to format the message.

func DebugC added in v1.3.0

func DebugC(ctx context.Context, args ...any)

DebugC logs a debug-level message with context using the global logger. ctx: The context for the log entry. msg: The message to log. args: Additional arguments to format the message.

func Error

func Error(args ...any)

Error logs an error-level message using the global logger.

func ErrorC added in v1.3.0

func ErrorC(ctx context.Context, args ...interface{})

ErrorC logs an error-level message with context using the global logger. ctx: The context for the log entry..

func Fatal

func Fatal(args ...any)

Fatal logs a fatal-level message using the global logger and then calls os.Exit(1).

func FatalC added in v1.3.0

func FatalC(ctx context.Context, args ...any)

FatalC logs a fatal-level message with context using the global logger and then calls os.Exit(1). ctx: The context for the log entry.

func Info

func Info(args ...interface{})
Example (Info)
package main

import (
	"github.com/jgolang/log"
)

func main() {
	// Use this function to see the trace of execution of the sentence.
	// This function is useful for tracking where errors are generated.
	log.Info("Hello world!")
}
Output:

{"level":"info","ts":"1599292300.656843","flags":"","caller":"hello.com/package/file.go:10","msg":"Hello world!"}

func InfoC added in v1.3.0

func InfoC(ctx context.Context, args ...interface{})

func Level added in v1.3.6

func Level() slog.Level

Level return current log level

func NewJSONHandler added in v1.3.0

func NewJSONHandler()

func NewTextHandler added in v1.3.0

func NewTextHandler()

func Panic

func Panic(args ...any)

Panic logs a panic-level message using the global logger and then panics.

func PanicC added in v1.3.0

func PanicC(ctx context.Context, args ...any)

PanicC logs a panic-level message with context using the global logger and then panics. ctx: The context for the log entry.

func Print added in v1.3.0

func Print(args ...interface{})

func PrintC added in v1.3.3

func PrintC(ctx context.Context, args ...interface{})

func SetCalldepth added in v1.1.2

func SetCalldepth(calldepth int)

SetCalldepth configure the number of stack frames to ascend, with 0 identifying the caller of Caller for default loggin

func SetLevel added in v1.3.0

func SetLevel(newLevel slog.Level) (oldLevel slog.Level)

SetLevel sets the logging level for the Logger instance. This method updates the log level to the specified level and returns the previous log level.

Parameters:

level (slog.Level) - The new log level to be set. This determines the severity of the logs
                     that will be captured. Common log levels include DEBUG, INFO, WARN, and ERROR.

Returns:

oldLevel (slog.Level) - The previous log level before it was updated. This can be used to restore
                        the previous log level if needed.

Example usage:

logger := &Logger{}
oldLevel := logger.SetLevel(slog.INFO)
// The log level is now set to INFO
// You can restore the old level if needed
logger.SetLevel(oldLevel)

func StackTrace

func StackTrace() slog.Attr

StackTrace allows you to view the exact place where the error or incident originated within the code. Shows a trace of up to 10 layers from where the error or incident was generated.

func Warn

func Warn(args ...any)

Warn logs a warning-level message using the global logger. msg: The message to log. args: Additional arguments to format the message.

func WarnC added in v1.3.0

func WarnC(ctx context.Context, args ...interface{})

WarnC logs a warning-level message with context using the global logger. ctx: The context for the log entry. msg: The message to log. args: Additional arguments to format the message.

Types

This section is empty.

Directories

Path Synopsis
core module

Jump to

Keyboard shortcuts

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