Documentation
¶
Overview ¶
Package entry provides a flexible, chainable logger entry wrapper for structured logging with logrus.
Overview ¶
The entry package wraps logrus entries to provide a fluent, chainable API for constructing structured log entries with context information, custom fields, errors, and arbitrary data. It supports integration with Gin web framework for automatic error registration.
Design Philosophy ¶
The package follows these core principles:
Immutability Pattern: All setter methods return the entry itself, enabling method chaining while maintaining a fluent API design.
Lazy Evaluation: The actual logging to logrus is deferred until the Log() or Check() method is called, allowing entries to be built incrementally.
Flexible Context: Entries can include timestamps, stack traces, caller information, file/line numbers, custom fields, arbitrary data, and multiple errors.
Safety First: All methods handle nil entries gracefully, returning nil or appropriate defaults rather than panicking.
Key Features ¶
- Fluent API: Chain methods for concise entry construction
- Multiple log levels: Debug, Info, Warn, Error, Fatal, Panic, and Nil
- Rich context: Time, stack, caller, file, line, and message
- Custom fields: Add structured key-value pairs
- Error handling: Multiple errors with nil filtering
- Data attachment: Attach arbitrary data structures
- Gin integration: Automatic error registration in Gin context
- Message-only mode: Simple logging without structured fields
Architecture ¶
The package consists of the following components:
┌──────────────────────────────────────┐
│ Entry Interface │
│ - Configuration methods │
│ - Field management │
│ - Error management │
│ - Logging methods │
└──────────────┬───────────────────────┘
│
▼
┌──────────────────────────────────────┐
│ entry Implementation │
│ │
│ ┌────────────────────────────────┐ │
│ │ Configuration State │ │
│ │ - Logger function │ │
│ │ - Gin context pointer │ │
│ │ - Message-only flag │ │
│ └────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────────────────────┐ │
│ │ Context Information │ │
│ │ - Time, Stack, Caller │ │
│ │ - File, Line, Message │ │
│ └────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌────────────────────────────────┐ │
│ │ Data & Fields │ │
│ │ - Custom fields │ │
│ │ - Errors slice │ │
│ │ - Arbitrary data │ │
│ └────────────────────────────────┘ │
│ │ │
│ ▼ │
│ Log to logrus │
└──────────────────────────────────────┘
Entry Lifecycle ¶
A typical entry follows this lifecycle:
- Creation: New(level) creates an entry with initial state
- Configuration: Set logger, level, gin context, message mode
- Context: Set time, stack, caller, file, line, message
- Fields: Add, merge, or set custom structured fields
- Errors: Add or set error information
- Data: Attach arbitrary data structures
- Logging: Call Log() or Check() to output to logrus
Logging Behavior ¶
The Log() method behavior depends on the entry configuration:
Normal Mode (clean=false):
- Includes all context fields (time, stack, caller, file, line)
- Includes custom fields and data
- Formats errors as comma-separated string
- Respects log level filtering
- Registers errors in Gin context if configured
Message-Only Mode (clean=true):
- Outputs only the message text
- Ignores all context fields and custom fields
- Uses simple Info level logging
- Suitable for console output or simple logs
Special Cases:
- NilLevel entries are never logged
- Nil logger or nil fields prevent logging
- FatalLevel triggers os.Exit(1) after logging
Level Hierarchy ¶
Log levels from most to least verbose:
- PanicLevel: System panic situations
- FatalLevel: Fatal errors (triggers exit)
- ErrorLevel: Error conditions
- WarnLevel: Warning conditions
- InfoLevel: Informational messages
- DebugLevel: Debug information
- TraceLevel: Detailed trace information
- NilLevel: Disabled logging
Field Management ¶
Fields are managed through the logger/fields package and provide:
- Type-safe key-value storage
- Merge operations for combining field sets
- Deletion of specific keys
- Integration with logrus.Fields
Fields must be initialized with FieldSet() before using FieldAdd(), FieldMerge(), or FieldClean().
Error Handling ¶
The package supports multiple error patterns:
Standard Errors:
- Add individual errors with ErrorAdd()
- Set entire error slice with ErrorSet()
- Clear errors with ErrorClean()
Error Filtering:
- ErrorAdd(cleanNil=true, ...) filters out nil errors
- ErrorAdd(cleanNil=false, ...) includes nil errors
Wrapped Errors:
- Automatically unwraps github.com/nabbar/golib/errors
- Extracts error slices from wrapped errors
- Supports fmt.Errorf %w wrapping
Integration Points ¶
Logrus Integration:
- Requires logger function returning *logrus.Logger
- Uses logrus.Entry for actual logging
- Supports all logrus formatters and hooks
Gin Integration:
- SetGinContext() enables automatic error registration
- Errors are added to Gin context error slice
- Useful for HTTP error handling and middleware
Performance Considerations ¶
Memory:
- Entry struct is lightweight (~300 bytes base)
- Fields and errors use slice allocation
- No memory pooling (entries are short-lived)
CPU:
- Method chaining has minimal overhead
- Field operations delegate to logger/fields package
- Log() method performs string building and formatting
Concurrency:
- Entries are NOT thread-safe
- Create separate entry per goroutine
- Logger function should return thread-safe logger
Use Cases ¶
Simple Logging:
entry.New(loglvl.InfoLevel).
SetLogger(loggerFunc).
FieldSet(fields).
SetEntryContext(time.Now(), 0, "", "", 0, "Hello").
Log()
Error Logging:
entry.New(loglvl.ErrorLevel).
SetLogger(loggerFunc).
FieldSet(fields).
ErrorAdd(true, err1, err2).
SetEntryContext(time.Now(), 0, "func", "file.go", 42, "Failed").
Log()
Structured Logging with Data:
entry.New(loglvl.InfoLevel).
SetLogger(loggerFunc).
FieldSet(fields).
FieldAdd("user_id", userID).
DataSet(requestData).
SetEntryContext(time.Now(), 0, "", "", 0, "Request processed").
Log()
Gin Error Registration:
e := entry.New(loglvl.ErrorLevel).
SetLogger(loggerFunc).
SetGinContext(c).
FieldSet(fields).
ErrorAdd(true, dbError).
SetEntryContext(time.Now(), 0, "", "", 0, "Database failed")
e.Log() // Registers error in c.Errors
Conditional Logging:
e := entry.New(loglvl.ErrorLevel).
SetLogger(loggerFunc).
FieldSet(fields).
ErrorAdd(true, err)
if e.Check(loglvl.InfoLevel) {
// Has errors, logged at ErrorLevel
} else {
// No errors, logged at InfoLevel
}
Best Practices ¶
DO:
- Always call FieldSet() before FieldAdd/FieldMerge/FieldClean
- Use method chaining for concise entry construction
- Filter nil errors with cleanNil=true in production
- Create new entry per log statement
- Set logger function that returns valid logger
DON'T:
- Share entries across goroutines
- Call FieldAdd/FieldMerge/FieldClean without FieldSet
- Ignore FatalLevel effects (os.Exit)
- Mutate fields/errors after logging
- Use PanicLevel in production code
Limitations ¶
- No automatic caller detection (must provide manually)
- No log buffering or batching
- No built-in sampling or rate limiting
- Fields must be initialized before use
- Not thread-safe (by design)
Dependencies ¶
Standard Library:
- os: For exit on fatal level
- strings: For error message joining
- time: For timestamp handling
External:
- github.com/sirupsen/logrus: Core logging engine
- github.com/gin-gonic/gin: Gin framework integration
- github.com/nabbar/golib/logger/fields: Field management
- github.com/nabbar/golib/logger/level: Log level definitions
- github.com/nabbar/golib/logger/types: Type constants
- github.com/nabbar/golib/errors: Error utilities
Package Status ¶
This package is production-ready and stable. It is widely used in nabbar/golib ecosystem for structured logging across multiple packages.
Example (BasicLogging) ¶
Example_basicLogging demonstrates the simplest usage of the entry package for basic structured logging with logrus.
package main
import (
"time"
logent "github.com/nabbar/golib/logger/entry"
logfld "github.com/nabbar/golib/logger/fields"
loglvl "github.com/nabbar/golib/logger/level"
"github.com/sirupsen/logrus"
)
func main() {
// Create a logger
logger := logrus.New()
logger.SetLevel(logrus.InfoLevel)
// Create fields for structured data
fields := logfld.New(nil)
// Create and log an entry
logent.New(loglvl.InfoLevel).
SetLogger(func() *logrus.Logger { return logger }).
FieldSet(fields).
SetEntryContext(time.Now(), 0, "", "", 0, "Application started").
Log()
}
Example (ComplexWorkflow) ¶
Example_complexWorkflow demonstrates a complete, complex logging workflow combining all major features.
package main
import (
"time"
logent "github.com/nabbar/golib/logger/entry"
logfld "github.com/nabbar/golib/logger/fields"
loglvl "github.com/nabbar/golib/logger/level"
"github.com/sirupsen/logrus"
)
func main() {
// Setup
logger := logrus.New()
logger.SetFormatter(&logrus.JSONFormatter{})
baseFields := logfld.New(nil)
baseFields.Add("service", "payment-api")
baseFields.Add("environment", "production")
// Simulate processing
userID := 12345
amount := 99.99
var processingErr error
// Build comprehensive log entry
entry := logent.New(loglvl.InfoLevel).
SetLogger(func() *logrus.Logger { return logger }).
FieldSet(baseFields).
FieldAdd("user_id", userID).
FieldAdd("amount", amount).
FieldAdd("currency", "USD")
// Add transaction data
txData := map[string]interface{}{
"transaction_id": "tx-789",
"payment_method": "credit_card",
"timestamp": time.Now().Unix(),
}
entry.DataSet(txData)
// Check for errors
if processingErr != nil {
entry.SetLevel(loglvl.ErrorLevel).
ErrorAdd(true, processingErr).
SetEntryContext(time.Now(), 0, "ProcessPayment", "payment.go", 156, "Payment processing failed")
} else {
entry.SetEntryContext(time.Now(), 0, "ProcessPayment", "payment.go", 180, "Payment processed successfully")
}
// Log the entry
entry.Log()
}
Example (ConditionalLogging) ¶
Example_conditionalLogging demonstrates using Check() for conditional logging with different levels based on error presence.
package main
import (
"fmt"
logent "github.com/nabbar/golib/logger/entry"
logfld "github.com/nabbar/golib/logger/fields"
loglvl "github.com/nabbar/golib/logger/level"
"github.com/sirupsen/logrus"
)
func main() {
logger := logrus.New()
fields := logfld.New(nil)
// Simulate operation that may fail
var err error
// err = performOperation()
entry := logent.New(loglvl.ErrorLevel).
SetLogger(func() *logrus.Logger { return logger }).
FieldSet(fields).
ErrorAdd(true, err)
// Check will log at ErrorLevel if errors exist, InfoLevel otherwise
hasErrors := entry.Check(loglvl.InfoLevel)
if hasErrors {
fmt.Println("Operation failed with errors")
} else {
fmt.Println("Operation succeeded")
}
}
Example (ContextInformation) ¶
Example_contextInformation demonstrates logging with detailed context information including stack traces, caller information, and file/line numbers.
package main
import (
"time"
logent "github.com/nabbar/golib/logger/entry"
logfld "github.com/nabbar/golib/logger/fields"
loglvl "github.com/nabbar/golib/logger/level"
"github.com/sirupsen/logrus"
)
func main() {
logger := logrus.New()
fields := logfld.New(nil)
// Log with full context information
logent.New(loglvl.DebugLevel).
SetLogger(func() *logrus.Logger { return logger }).
FieldSet(fields).
SetEntryContext(
time.Now(), // timestamp
12345, // goroutine stack number
"ProcessRequest", // caller function name
"handler.go", // file name
78, // line number
"Processing user request", // message
).
Log()
}
Example (DataAttachment) ¶
Example_dataAttachment demonstrates attaching arbitrary data structures.
package main
import (
"time"
logent "github.com/nabbar/golib/logger/entry"
logfld "github.com/nabbar/golib/logger/fields"
loglvl "github.com/nabbar/golib/logger/level"
"github.com/sirupsen/logrus"
)
func main() {
logger := logrus.New()
fields := logfld.New(nil)
// Create data structure
data := map[string]interface{}{
"request_id": "req-123",
"duration_ms": 450,
"status_code": 200,
}
logent.New(loglvl.InfoLevel).
SetLogger(func() *logrus.Logger { return logger }).
FieldSet(fields).
DataSet(data).
SetEntryContext(time.Now(), 0, "", "", 0, "Request completed").
Log()
}
Example (ErrorLogging) ¶
Example_errorLogging demonstrates logging with error information.
package main
import (
"errors"
"time"
logent "github.com/nabbar/golib/logger/entry"
logfld "github.com/nabbar/golib/logger/fields"
loglvl "github.com/nabbar/golib/logger/level"
"github.com/sirupsen/logrus"
)
func main() {
logger := logrus.New()
logger.SetLevel(logrus.ErrorLevel)
fields := logfld.New(nil)
// Create an error entry
err := errors.New("database connection failed")
logent.New(loglvl.ErrorLevel).
SetLogger(func() *logrus.Logger { return logger }).
FieldSet(fields).
ErrorAdd(true, err). // cleanNil=true filters out nil errors
SetEntryContext(time.Now(), 0, "ConnectDB", "db.go", 42, "Failed to connect to database").
Log()
}
Example (ErrorManagement) ¶
Example_errorManagement demonstrates various error management operations.
package main
import (
"errors"
"time"
logent "github.com/nabbar/golib/logger/entry"
logfld "github.com/nabbar/golib/logger/fields"
loglvl "github.com/nabbar/golib/logger/level"
"github.com/sirupsen/logrus"
)
func main() {
logger := logrus.New()
fields := logfld.New(nil)
// Create entry
entry := logent.New(loglvl.ErrorLevel).
SetLogger(func() *logrus.Logger { return logger }).
FieldSet(fields)
// Add errors incrementally
entry.ErrorAdd(true, errors.New("first error"))
entry.ErrorAdd(true, errors.New("second error"))
// Log current errors
entry.SetEntryContext(time.Now(), 0, "", "", 0, "Multiple errors").Log()
// Clean errors and add new ones
entry.ErrorClean().
ErrorAdd(true, errors.New("new error after cleanup")).
SetEntryContext(time.Now(), 0, "", "", 0, "After cleanup").
Log()
// Set errors directly with a slice
errs := []error{
errors.New("error from slice 1"),
errors.New("error from slice 2"),
}
entry.ErrorSet(errs).
SetEntryContext(time.Now(), 0, "", "", 0, "Set errors").
Log()
}
Example (FieldManagement) ¶
Example_fieldManagement demonstrates managing custom fields throughout entry lifecycle.
package main
import (
"time"
logent "github.com/nabbar/golib/logger/entry"
logfld "github.com/nabbar/golib/logger/fields"
loglvl "github.com/nabbar/golib/logger/level"
"github.com/sirupsen/logrus"
)
func main() {
logger := logrus.New()
// Create base fields
baseFields := logfld.New(nil)
baseFields.Add("app", "myapp")
baseFields.Add("version", "1.0.0")
// Create additional fields
reqFields := logfld.New(nil)
reqFields.Add("request_id", "req-456")
// Build entry with merged fields
entry := logent.New(loglvl.InfoLevel).
SetLogger(func() *logrus.Logger { return logger }).
FieldSet(baseFields).
FieldAdd("endpoint", "/api/users").
FieldMerge(reqFields).
SetEntryContext(time.Now(), 0, "", "", 0, "API request")
// Log the entry
entry.Log()
// Clean specific fields for reuse
entry.FieldClean("request_id").
FieldAdd("request_id", "req-457").
SetEntryContext(time.Now(), 0, "", "", 0, "Next request").
Log()
}
Example (LevelControl) ¶
Example_levelControl demonstrates controlling log levels dynamically.
package main
import (
"errors"
"time"
logent "github.com/nabbar/golib/logger/entry"
logfld "github.com/nabbar/golib/logger/fields"
loglvl "github.com/nabbar/golib/logger/level"
"github.com/sirupsen/logrus"
)
func main() {
logger := logrus.New()
logger.SetLevel(logrus.DebugLevel) // Allow all levels
fields := logfld.New(nil)
// Create entry and change level
entry := logent.New(loglvl.DebugLevel).
SetLogger(func() *logrus.Logger { return logger }).
FieldSet(fields)
// Log at debug level
entry.SetEntryContext(time.Now(), 0, "", "", 0, "Debug message").Log()
// Change to info level and log again
entry.SetLevel(loglvl.InfoLevel).
SetEntryContext(time.Now(), 0, "", "", 0, "Info message").
Log()
// Change to error level
entry.SetLevel(loglvl.ErrorLevel).
ErrorAdd(true, errors.New("error occurred")).
SetEntryContext(time.Now(), 0, "", "", 0, "Error message").
Log()
}
Example (MessageOnly) ¶
Example_messageOnly demonstrates simple message-only logging without structured fields.
package main
import (
"time"
logent "github.com/nabbar/golib/logger/entry"
logfld "github.com/nabbar/golib/logger/fields"
loglvl "github.com/nabbar/golib/logger/level"
"github.com/sirupsen/logrus"
)
func main() {
logger := logrus.New()
fields := logfld.New(nil)
// Log only the message, ignoring all fields
logent.New(loglvl.InfoLevel).
SetLogger(func() *logrus.Logger { return logger }).
FieldSet(fields).
SetMessageOnly(true).
SetEntryContext(time.Now(), 0, "", "", 0, "Simple console message").
Log()
}
Example (MethodChaining) ¶
Example_methodChaining demonstrates the fluent API for building complex entries.
package main
import (
"errors"
"time"
logent "github.com/nabbar/golib/logger/entry"
logfld "github.com/nabbar/golib/logger/fields"
loglvl "github.com/nabbar/golib/logger/level"
"github.com/sirupsen/logrus"
)
func main() {
logger := logrus.New()
fields := logfld.New(nil)
// Build complex entry with method chaining
logent.New(loglvl.WarnLevel).
SetLogger(func() *logrus.Logger { return logger }).
FieldSet(fields).
FieldAdd("component", "auth").
FieldAdd("attempt", 3).
ErrorAdd(true, errors.New("invalid credentials")).
DataSet(map[string]string{"username": "user@example.com"}).
SetEntryContext(time.Now(), 0, "Authenticate", "auth.go", 100, "Authentication failed").
Log()
}
Example (MultipleErrors) ¶
Example_multipleErrors demonstrates handling multiple errors in a single entry.
package main
import (
"errors"
"time"
logent "github.com/nabbar/golib/logger/entry"
logfld "github.com/nabbar/golib/logger/fields"
loglvl "github.com/nabbar/golib/logger/level"
"github.com/sirupsen/logrus"
)
func main() {
logger := logrus.New()
fields := logfld.New(nil)
// Collect multiple errors
err1 := errors.New("connection timeout")
err2 := errors.New("retry failed")
logent.New(loglvl.ErrorLevel).
SetLogger(func() *logrus.Logger { return logger }).
FieldSet(fields).
ErrorAdd(true, err1, err2).
SetEntryContext(time.Now(), 0, "", "", 0, "Multiple failures occurred").
Log()
}
Example (StructuredData) ¶
Example_structuredData demonstrates adding custom structured data to log entries.
package main
import (
"time"
logent "github.com/nabbar/golib/logger/entry"
logfld "github.com/nabbar/golib/logger/fields"
loglvl "github.com/nabbar/golib/logger/level"
"github.com/sirupsen/logrus"
)
func main() {
logger := logrus.New()
// Create fields with custom data
fields := logfld.New(nil)
// Create entry with custom fields
logent.New(loglvl.InfoLevel).
SetLogger(func() *logrus.Logger { return logger }).
FieldSet(fields).
FieldAdd("user_id", 12345).
FieldAdd("action", "login").
FieldAdd("ip_address", "192.168.1.1").
SetEntryContext(time.Now(), 0, "", "", 0, "User logged in").
Log()
}
Index ¶
Examples ¶
- Package (BasicLogging)
- Package (ComplexWorkflow)
- Package (ConditionalLogging)
- Package (ContextInformation)
- Package (DataAttachment)
- Package (ErrorLogging)
- Package (ErrorManagement)
- Package (FieldManagement)
- Package (LevelControl)
- Package (MessageOnly)
- Package (MethodChaining)
- Package (MultipleErrors)
- Package (StructuredData)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entry ¶
type Entry interface {
// SetLogger sets the logger function of the entry. The logger function must return
// a pointer to a logrus.Logger object. If the logger function is nil, the
// entry will not log anything.
//
// Example:
// e := New(loglvl.InfoLevel)
// e = e.SetLogger(func() *logrus.Logger { return logrus.New() })
SetLogger(fct func() *logrus.Logger) Entry
// SetLevel sets the level of the entry. The level determines when the entry is
// logged. If the level is lower than the logger's level, the entry will not
// be logged. If the level is loglvl.NilLevel, the entry will not be logged
// regardless of the logger's level.
//
// Example:
// e := New(loglvl.InfoLevel)
// e = e.SetLevel(loglvl.WarnLevel)
SetLevel(lvl loglvl.Level) Entry
// SetMessageOnly sets a flag to log only the message of the entry. If the
// flag is true, the entry will only log the message. If the flag is
// false, the entry will log all the fields and the message.
//
// Example:
// e := New(loglvl.InfoLevel)
// e = e.SetMessageOnly(true)
SetMessageOnly(flag bool) Entry
// SetEntryContext sets the context fields of the entry. The context fields are
// logged together with the message. The context fields are: time, stack,
// caller, file, line, and message.
//
// Example:
// e := New(loglvl.InfoLevel)
// e = e.SetEntryContext(time.Now(), 123, "caller", "file.go", 456, "message")
SetEntryContext(etime time.Time, stack uint64, caller, file string, line uint64, msg string) Entry
// SetGinContext sets the gin context pointer of the entry. The gin context pointer
// is used to register the errors of the current entry into the gin context
// error slice. If the gin context pointer is nil, the entry will not register any
// errors into the gin context error slice.
//
// Example:
// e := New(loglvl.InfoLevel)
// e = e.SetGinContext(ginContext)
//
// This function is useful when you want to log errors of your application and
// register them into the gin context error slice.
SetGinContext(ctx *ginsdk.Context) Entry
// DataSet sets the data of the entry. The data is logged together with the
// context fields and the message. The data can be any type that can be
// marshaled into a JSON object. If the data is nil, no data will be logged.
//
// Example:
// e := New(loglvl.InfoLevel)
// e = e.DataSet(map[string]string{"key1": "value1", "key2": "value2"})
//
// This function is useful when you want to log additional data of your application
// and you want to log it in a structured way.
DataSet(data interface{}) Entry
// Check returns true if the level of the entry is greater than or equal to
// lvlNoErr, and false otherwise.
//
// Example:
// e := New(loglvl.InfoLevel)
// result := e.Check(loglvl.WarnLevel)
//
// This function is useful when you want to check if the entry should be logged
// based on the level of the entry and the logger's level.
Check(lvlNoErr loglvl.Level) bool
// Log logs the entry into the logger. The entry is logged together with the
// context fields and the message. If the logger is nil, the entry will not
// be logged. If the logger's level is lower than the entry's level, the entry
// will not be logged. If the logger's level is loglvl.NilLevel, the entry will
// not be logged regardless of the entry's level.
//
// Example:
// e := New(loglvl.InfoLevel)
// e = e.SetLogger(func() *logrus.Logger { return logrus.New() })
// e.Log()
Log()
// FieldAdd adds a new field to the entry. The field is a couple of a key
// (string) and a value (interface{}). The value can be of any type that
// can be marshaled into a JSON object. The key must be unique, if the
// key already exists, it will be overwritten with the new value.
//
// Example:
// e := New(loglvl.InfoLevel)
// e = e.FieldAdd("key", "value")
//
// This function is useful when you want to log additional data of your
// application and you want to log it in a structured way.
FieldAdd(key string, val interface{}) Entry
// FieldMerge merges the fields of another logfld.Fields object into the
// current entry. The fields are merged in a shallow way, meaning
// that if the key already exists in the current entry, it will be
// overwritten with the new value from the other logfld.Fields object.
//
// Example:
// e := New(loglvl.InfoLevel)
// fields1 := logfld.New(nil)
// fields1.Add("key1", "value1")
// fields2 := logfld.New(nil)
// fields2.Add("key2", "value2")
// e = e.FieldMerge(fields1)
// e = e.FieldMerge(fields2)
//
// This function is useful when you want to log additional data of your
// application and you want to log it in a structured way.
FieldMerge(fields logfld.Fields) Entry
// FieldSet sets the fields of the entry. The fields are a map of key-value
// pairs where the key is a string and the value is an interface{}.
// The fields are logged together with the context fields and the message.
//
// Example:
// e := New(loglvl.InfoLevel)
// fields := logfld.New(nil)
// fields.Add("key1", "value1")
// fields.Add("key2", 123)
// e = e.FieldSet(fields)
//
// This function is useful when you want to log additional data of your
// application and you want to log it in a structured way.
FieldSet(fields logfld.Fields) Entry
// FieldClean removes the fields specified by the keys from the entry. If
// the key does not exist, it is ignored. If the keys argument is empty,
// this function does nothing and returns the entry unchanged.
//
// Example:
// e := New(loglvl.InfoLevel)
// fields := logfld.New(nil)
// fields.Add("key1", "value1")
// fields.Add("key2", "value2")
// e = e.FieldSet(fields)
// e = e.FieldClean("key1")
//
// This function is useful when you want to log additional data of your
// application and you want to log it in a structured way.
FieldClean(keys ...string) Entry
// ErrorClean removes all errors from the entry. If the entry has no
// errors, this function does nothing and returns the entry unchanged.
//
// Example:
// e := New(loglvl.InfoLevel)
// e = e.ErrorAdd(errors.New("error1"), errors.New("error2"))
// e = e.ErrorClean()
//
// This function is useful when you want to clear all errors from the entry
// and you want to log the entry in a structured way.
ErrorClean() Entry
// ErrorSet sets the errors of the entry. The errors are logged together with
// the context fields and the message. If the errors argument is empty,
// this function does nothing and returns the entry unchanged.
//
// Example:
// e := New(loglvl.InfoLevel)
// e = e.ErrorSet([]error{errors.New("error1"), errors.New("error2")})
//
// This function is useful when you want to log errors of your application
// and you want to log them in a structured way.
ErrorSet(err []error) Entry
// ErrorAdd adds the errors to the entry. The errors are logged together with
// the context fields and the message. If the cleanNil argument is true, all
// nil errors are removed from the entry. If the cleanNil argument is false,
// all errors are added to the entry regardless of whether they are nil or not.
//
// Example:
// e := New(loglvl.InfoLevel)
// e = e.ErrorAdd(true, errors.New("error1"), nil)
//
// This function is useful when you want to log errors of your application
// and you want to log them in a structured way.
ErrorAdd(cleanNil bool, err ...error) Entry
}
func New ¶
New returns a new Entry with the given level. The new Entry will be set with the current time and an empty error slice. The new Entry will also have a nil logger and gin context, and will not be cleaned. The new Entry will have no additional data and will have no additional fields.
Example:
e := New(loglvl.InfoLevel)
e = e.ErrorAdd(errors.New("error1"), errors.New("error2"))
This function is useful when you want to log messages of your application and you want to log them in a structured way.