logging

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: GPL-3.0 Imports: 8 Imported by: 5

README

Simple GO logger

Simple output log with UUID according by context.Context parameter.

When logger is attached it's generate random global UUID.
This approach is used for services performing parallel tasks in order to separate processes.
When functions are called without a context.Context parameter, a global UUID value is used.

Example

Usage:

package main

import (
	"context"

	"github.com/google/uuid"
	"github.com/ra-company/logging"
)

func main() {
	ctx := context.Background()

	title := "Sample"

	logging.Logs.Infof(ctx, "Service %s was started.", title)

	id := uuid.New().String()
	ctx = context.WithValue(ctx, logging.CtxKeyUUID, id)

	logging.Logs.Debugf(ctx, "Log data with UUID: %s.", id)

	logging.Logs.Error("CTX isn't used.")
}

Sample output:

2025/06/17 18:17:42.016 INF     [f4d14d28-ae09-4aed-958a-c6dcb6da2a89]  Service Sample was started.
2025/06/17 18:17:42.017 DBG     [3d861cf8-ab1c-4d6d-b91e-ba17027a0045]  Log data with UUID: 3d861cf8-ab1c-4d6d-b91e-ba17027a0045.
2025/06/17 18:17:42.018 ERR     [f4d14d28-ae09-4aed-958a-c6dcb6da2a89]  CTX isn't used.

Using Graylog as log server

The system can send log data to a Graylog server. To enable logging to Graylog, the following global variables must be configured:

GraylogAddr string = ""               // Graylog address (when empty, Graylog is disabled)
Application string = "go-application" // Application name
Host        string = "localhost"      // Host name
Facility    string = "facility"       // Facility name

Staying up to date

To update library to the latest version, use go get -u github.com/ra-company/logging.

Supported go versions

We currently support the most recent major Go versions from 1.25.5 onward.

License

This project is licensed under the terms of the GPL-3.0 license.

Used libraries

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Logs        Logging
	CtxKeyUUID  CtxKey = "process-uuid"   // Context key for process UUID
	GraylogAddr string = ""               // Graylog address (when empty, Graylog is disabled)
	Application string = "go-application" // Application name
	Host        string = "localhost"      // Host name
	Facility    string = "facility"       // Facility name
)

Functions

This section is empty.

Types

type CtxKey

type CtxKey string

Context key type

type CustomLogger added in v1.0.4

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

CustomLogger is a wrapper around a Logger interface that allows for custom logging implementations. It provides methods to log messages

Example
cLog := &Logging{
	LogLevel:   0,
	UUID:       "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049",
	ShowTime:   false,
	ConsoleApp: false,
	DontStop:   true, // Prevent exit on fatal error
}

logs := &CustomLogger{}
logs.SetLogger(cLog)

Logs.LogLevel = 0
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd050"
Logs.ShowTime = false
Logs.Starting("test")
Logs.DontStop = true // Prevent exit on fatal error
defer Logs.Stopping()

ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")

world := "World"

Logs.Debug(ctx, "Standard Logger with ctx and variable: %s", world)
Logs.Debug(ctx, "Standard Logger with ctx without variable")
Logs.Debug("Standard Logger without ctx and variable: %s", world)
Logs.Debug("Standard Logger without ctx without variable")

logs.Debug(ctx, "CustomerLogger with ctx and variable: %s", world)
logs.Debug(ctx, "CustomerLogger with ctx without variable")
logs.Debug("CustomerLogger without ctx with variable: %s", world)
logs.Debug("CustomerLogger without ctx without variable")

logs.Info(ctx, "CustomerLogger with ctx and variable: %s", world)
logs.Info(ctx, "CustomerLogger with ctx without variable")
logs.Info("CustomerLogger without ctx with variable: %s", world)
logs.Info("CustomerLogger without ctx without variable")

logs.Warn(ctx, "CustomerLogger with ctx and variable: %s", world)
logs.Warn(ctx, "CustomerLogger with ctx without variable")
logs.Warn("CustomerLogger without ctx with variable: %s", world)
logs.Warn("CustomerLogger without ctx without variable")

logs.Error(ctx, "CustomerLogger with ctx and variable: %s", world)
logs.Error(ctx, "CustomerLogger with ctx without variable")
logs.Error("CustomerLogger without ctx with variable: %s", world)
logs.Error("CustomerLogger without ctx without variable")

logs.Fatal(ctx, "CustomerLogger with ctx and variable: %s", world)
Output:
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd050]	test service is starting...
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd050]	test service is stopping...
DBG	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Standard Logger with ctx and variable: World
DBG	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Standard Logger with ctx without variable
DBG	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd050]	Standard Logger without ctx and variable: World
DBG	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd050]	Standard Logger without ctx without variable
DBG	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	CustomerLogger with ctx and variable: World
DBG	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	CustomerLogger with ctx without variable
DBG	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	CustomerLogger without ctx with variable: World
DBG	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	CustomerLogger without ctx without variable
INF	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	CustomerLogger with ctx and variable: World
INF	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	CustomerLogger with ctx without variable
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	CustomerLogger without ctx with variable: World
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	CustomerLogger without ctx without variable
WRN	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	CustomerLogger with ctx and variable: World
WRN	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	CustomerLogger with ctx without variable
WRN	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	CustomerLogger without ctx with variable: World
WRN	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	CustomerLogger without ctx without variable
ERR	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	CustomerLogger with ctx and variable: World
ERR	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	CustomerLogger with ctx without variable
ERR	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	CustomerLogger without ctx with variable: World
ERR	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	CustomerLogger without ctx without variable
FTL	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	CustomerLogger with ctx and variable: World

func (*CustomLogger) Debug added in v1.0.4

func (dst *CustomLogger) Debug(args ...any)

Debug logs a debug message using the provided logger or the default logging mechanism. It is useful for debugging purposes and can be used to log detailed information about the API calls and their responses.

Parameters:

  • args: The debug message to log, which can be any number of arguments. # args[0] - context (optional) or argument to print # args[1:] - additional arguments to print

func (*CustomLogger) Error added in v1.0.4

func (dst *CustomLogger) Error(args ...any)

Error logs an error message using the provided logger or the default logging mechanism. It is useful for logging errors encountered during API calls or other operations.

Parameters:

  • args: The error message to log, which can be any number of arguments. # args[0] - context (optional) or argument to print # args[1:] - additional arguments to print

func (*CustomLogger) Fatal added in v1.0.4

func (dst *CustomLogger) Fatal(args ...any)

Fatal logs a fatal error message using the provided logger or the default logging mechanism. It is useful for logging critical errors that require immediate attention and may cause the application to exit.

Parameters:

  • args: The fatal error message to log, which can be any number of arguments. # args[0] - context (optional) or argument to print # args[1:] - additional arguments to print

func (*CustomLogger) Info added in v1.0.4

func (dst *CustomLogger) Info(args ...any)

Info logs an informational message using the provided logger or the default logging mechanism. It is useful for logging general information about the API calls and their responses.

Parameters:

  • args: The informational message to log, which can be any number of arguments. # args[0] - context (optional) or argument to print # args[1:] - additional arguments to print

func (*CustomLogger) SetLogger added in v1.0.4

func (dst *CustomLogger) SetLogger(logger Logger)

SetLogger allows setting a custom logger. This is useful when you want to use a different logging mechanism instead of the default one provided by the package.

Parameters:

  • logger: An instance of a type that implements the Logger interface.

func (*CustomLogger) Warn added in v1.0.4

func (dst *CustomLogger) Warn(args ...any)

Warn logs a warning message using the provided logger or the default logging mechanism. It is useful for logging potential issues or unexpected behavior in the API calls.

Parameters:

  • args: The warning message to log, which can be any number of arguments. # args[0] - context (optional) or argument to print # args[1:] - additional arguments to print

type Logger added in v1.0.4

type Logger interface {
	// Debug logs a message at Debug level.
	// The first argument is the context for the log entry,
	Debug(args ...any)

	// Info logs a message at Info level.
	// The first argument is the context for the log entry,
	Info(args ...any)

	// Warn logs a message at Warning level.
	// The first argument is the context for the log entry,
	Warn(args ...any)

	// Error logs a message at Error level.
	// The first argument is the context for the log entry,
	Error(args ...any)

	// Fatal logs a message at Fatal level
	// and process will exit with status set to 1.
	// The first argument is the context for the log entry,
	Fatal(args ...any)
}

Logger Interface defines the methods for logging at different levels.

type Logging

type Logging struct {
	UUID       string
	LogLevel   int  // Log level (0 - debug, 1 - warning, 2 - error, 3 - fatal, default 0)
	ConsoleApp bool // Console application flag (do not print logs in console app)
	ShowTime   bool // Show time in logs
	DontStop   bool // Do not stop service on fatal error
	// contains filtered or unexported fields
}

func (*Logging) Debug

func (logger *Logging) Debug(args ...any)

Debug logs a debug message.

Parameters:

  • args - arguments to print # args[0] - context (optional) or argument to print # args[1:] - arguments to print
Example
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049"
Logs.Starting("test")
Logs.ShowTime = false
defer Logs.Stopping()

ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")

testCases := []int{0, 1, 2, 3}
for _, tc := range testCases {
	Logs.LogLevel = tc

	Logs.Debug("Hello World")
	Logs.Debugf("Hello %s", "Universe")
	Logs.Debug(ctx, "Hello World")
	Logs.Debugf(ctx, "Hello %s", "Universe")
}
Output:
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	test service is starting...
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	test service is stopping...
DBG	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
DBG	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
DBG	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
DBG	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe

func (*Logging) Debugf

func (logger *Logging) Debugf(args ...any)

Debugf logs a formatted debug message.

Parameters:

  • args - arguments to print # args[0] - context (optional) or format string # args[1] - format string (if args[0] is context) # args[2:] - arguments to format string

func (*Logging) Error

func (logger *Logging) Error(args ...any)

Error logs an error message.

Parameters:

  • args - arguments to print # args[0] - context (optional) or argument to print # args[1:] - arguments to print
Example
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049"
Logs.Starting("test")
Logs.ShowTime = false
defer Logs.Stopping()

ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")

testCases := []int{0, 1, 2, 3}
for _, tc := range testCases {
	Logs.LogLevel = tc

	Logs.Error("Hello World")
	Logs.Errorf("Hello %s", "Universe")
	Logs.Error(ctx, "Hello World")
	Logs.Errorf(ctx, "Hello %s", "Universe")
}
Output:
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	test service is starting...
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	test service is stopping...
ERR	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
ERR	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
ERR	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
ERR	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
ERR	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
ERR	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
ERR	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
ERR	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe
ERR	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
ERR	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe
ERR	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
ERR	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe

func (*Logging) Errorf

func (logger *Logging) Errorf(args ...any)

Errorf logs a formatted error message.

Parameters:

  • args - arguments to print # args[0] - context (optional) or format string # args[1] - format string (if args[0] is context) # args[2:] - arguments to format string

func (*Logging) Fatal

func (logger *Logging) Fatal(args ...any)

Fatal logs a fatal error message and exits the program.

Parameters:

  • args - arguments to print # args[0] - context (optional) or argument to print # args[1:] - arguments to print
Example
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049"
Logs.Starting("test")
Logs.ShowTime = false
Logs.DontStop = true // Prevent exit on fatal error
defer Logs.Stopping()

ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")

testCases := []int{0, 1, 2, 3}
for _, tc := range testCases {
	Logs.LogLevel = tc

	Logs.Fatal("Hello World")
	Logs.Fatalf("Hello %s", "Universe")
	Logs.Fatal(ctx, "Hello World")
	Logs.Fatalf(ctx, "Hello %s", "Universe")
}
Output:
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	test service is starting...
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	test service is stopping...
FTL	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
FTL	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
FTL	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
FTL	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
FTL	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
FTL	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
FTL	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
FTL	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
FTL	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
FTL	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe
FTL	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
FTL	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe
FTL	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
FTL	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe
FTL	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
FTL	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe

func (*Logging) Fatalf

func (logger *Logging) Fatalf(args ...any)

Fatalf logs a formatted fatal error message and exits the program.

Parameters:

  • args - arguments to print # args[0] - context (optional) or format string # args[1] - format string (if args[0] is context) # args[2:] - arguments to format string

func (*Logging) GetLevel

func (logger *Logging) GetLevel(level int, ctx any) (string, string, bool)

Get level of logging by level and context if it's present

Parameters:

  • level - log level
  • ctx - context

func (*Logging) Info

func (logger *Logging) Info(args ...any)

Info logs an informational message.

Parameters:

  • args - arguments to print # args[0] - context (optional) or argument to print # args[1:] - arguments to print
Example
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049"
Logs.Starting("test")
Logs.ShowTime = false
defer Logs.Stopping()

ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")

Logs.Info("Hello World")
Logs.Infof("Hello %s", "Universe")
Logs.Info(ctx, "Hello World")
Logs.Infof(ctx, "Hello %s", "Universe")
Output:
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	test service is starting...
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	test service is stopping...
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
INF	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
INF	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe

func (*Logging) Infof

func (logger *Logging) Infof(args ...any)

Infof logs a formatted informational message.

Parameters:

  • args - arguments to print # args[0] - context (optional) or format string # args[1] - format string (if args[0] is context) or argument to print # args[2:] - arguments to format string

func (*Logging) Print

func (logger *Logging) Print(level int, args ...any)

Print logs to console

Parameters:

  • level - log level (0 - debug, 1 - warning, 2 - error, 3 - fatal, 4 - info)
  • args - arguments to print
Example
Logs.LogLevel = 0
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049"
Logs.ShowTime = false
Logs.Starting("test")
defer Logs.Stopping()

ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")

testCases := []int{-1, 0, 1, 2, 3, 4, 5}

for _, tc := range testCases {
	Logs.Print(tc, "Hello World")
	Logs.Printf(tc, "Hello %s", "Universe")
	Logs.Print(tc, ctx, "Hello World")
	Logs.Printf(tc, ctx, "Hello %s", "Universe")
}
Output:
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	test service is starting...
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	test service is stopping...
DBG	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
WRN	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
ERR	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
FTL	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
DBG	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
WRN	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
ERR	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
FTL	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
DBG	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
WRN	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
ERR	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
FTL	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
INF	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
INF	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
INF	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
DBG	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe
WRN	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe
ERR	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe
FTL	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe
INF	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe
INF	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe
INF	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe

func (*Logging) Printf

func (logger *Logging) Printf(level int, args ...any)

Printf logs formatted output to console

Parameters:

  • level - log level (0 - debug, 1 - warning, 2 - error, 3 - fatal, 4 - info)
  • args - arguments to print # args[0] - format string # args[1:] - arguments to format string

func (*Logging) Starting

func (logger *Logging) Starting(title string)

Starting service

Parameters:

  • title - process title

func (*Logging) Stopping

func (logger *Logging) Stopping()

Stopping service

func (*Logging) TimeToStr

func (logger *Logging) TimeToStr(t time.Time) string

TimeToStr converts time.Time to string in format "2006/01/02 15:04:05.999" It ensures that the string is always 23 characters long by appending "00" or "0" as needed.

Parameters:

  • t - time.Time object to convert

Returns:

  • string: representation of the time in the specified format

func (*Logging) Warn

func (logger *Logging) Warn(args ...any)

Warn logs a warning message.

Parameters:

  • args - arguments to print # args[0] - context (optional) or argument to print # args[1:] - arguments to print
Example
Logs.UUID = "b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049"
Logs.Starting("test")
Logs.ShowTime = false
defer Logs.Stopping()

ctx := context.Background()
ctx = context.WithValue(ctx, CtxKeyUUID, "4577c272-e9b8-4a19-a9d0-4ec0bde6063f")

testCases := []int{0, 1, 2, 3}
for _, tc := range testCases {
	Logs.LogLevel = tc

	Logs.Warn("Hello World")
	Logs.Warnf("Hello %s", "Universe")
	Logs.Warn(ctx, "Hello World")
	Logs.Warnf(ctx, "Hello %s", "Universe")
}
Output:
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	test service is starting...
INF	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	test service is stopping...
WRN	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
WRN	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
WRN	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello World
WRN	[b846c7ab-9bc3-4c3a-b9e9-c65ae7bdd049]	Hello Universe
WRN	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
WRN	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe
WRN	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello World
WRN	[4577c272-e9b8-4a19-a9d0-4ec0bde6063f]	Hello Universe

func (*Logging) Warnf

func (logger *Logging) Warnf(args ...any)

Warnf logs a formatted warning message.

Parameters:

  • args - arguments to print # args[0] - context (optional) or format string # args[1] - format string (if args[0] is context) # args[2:] - arguments to format string

Jump to

Keyboard shortcuts

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