# README
tlog: The log module extracted from trpc-group/trpc-go
tlog is a lightweight and flexible logging library for Go applications, extracted from the trpc-group/trpc-go project. It provides a simple way to configure and manage logs in your applications with various output configurations and levels.
Features
- Multiple Output Configurations: Support for different writers like console and file.
- Log Level Management: Easily set log levels for different parts of your application.
- Custom Formatters: JSON formatting support for structured logging.
- Contextual Logging: Add context to your logs for better traceability.
Installation
To get started with tlog, simply install it using go get:
go get github.com/phpgao/tlog
Configuration
tlog allows you to configure multiple output destinations and set log levels and formatters for each. Here's an example configuration:
package main
import (
"github.com/phpgao/tlog"
"net/http"
)
func main() {
var c = []tlog.OutputConfig{
{
Writer: "console",
Level: "info",
Formatter: "json",
},
{
Writer: "file",
WriteConfig: tlog.WriteConfig{
LogPath: "/tmp/",
Filename: "trpc.log",
RollType: "size",
MaxAge: 1,
MaxBackups: 5,
Compress: false,
MaxSize: 1024,
},
Level: "debug",
Formatter: "json",
},
}
tlog.Register("default", tlog.NewZapLog(c))
tlog.Infof("Hello, tlog!")
tlog.SetLevel("0", tlog.LevelInfo)
}
Usage
tlog provides various methods to log messages at different levels. Here's how you can use it:
// Log an informational message
tlog.Infof("Info message: %s", "This is an info message")
// Log an error message
tlog.Errorf("Error message: %s", "This is an error message")
// Log a message with context
ctx := tlog.WithContextFields(context.TODO(), "key1", "value1", "key2", "value2")
tlog.InfoContextf(ctx, "Contextual message: %s", "This message has context")
// use RegisterHandler to set log level
mux := http.NewServeMux()
RegisterHandlerWithPath(mux, "/")
http.ListenAndServe(":8080", mux)
// gin logger
router.Use(handler.GinLogger(), gin.Recovery())
License
tlog is released under the Apache License Version 2.0.
Feel free to star and watch the repository to keep up with the latest updates!
# Packages
No description provided by the author
Package handler comes from admin/admin.go of trpc-go you can add this handler to your http(s) server.
Package rollwriter provides a high performance rolling file log.
# Functions
CustomTimeFormat customize time format.
Debug logs to DEBUG log.
DebugContext logs to DEBUG log.
DebugContextf logs to DEBUG log.
Debugf logs to DEBUG log.
DefaultTimeFormat returns the default time format "2006-01-02 15:04:05.000".
EnableTrace enables trace.
Error logs to ERROR log.
ErrorContext logs to ERROR log.
ErrorContextf logs to ERROR log.
Errorf logs to ERROR log.
Fatal logs to ERROR log.
FatalContext logs to ERROR log.
FatalContextf logs to ERROR log.
Fatalf logs to ERROR log.
Get returns the Logger implementation by log name.
GetDefaultLogger gets the default Logger.
GetLevel gets log level for different output.
GetLogEncoderKey gets user defined log output name, uses defKey if empty.
GetWriter gets log output writer, returns nil if not exist.
Info logs to INFO log.
InfoContext logs to INFO log.
InfoContextf logs to INFO log.
Infof logs to INFO log.
NewTimeEncoder creates a time format encoder.
NewZapLog creates a trpc default Logger from zap whose caller skip is set to 2.
NewZapLogWithCallerSkip creates a trpc default Logger from zap.
RedirectStdLog redirects std log to trpc logger as log level INFO.
RedirectStdLogAt redirects std log to trpc logger with a specific level.
Register registers Logger.
RegisterFormatEncoder registers a NewFormatEncoder with the specified formatName key.
RegisterWriter registers log output writer.
SetLevel sets log level for different output which may be "0", "1" or "2".
SetLogger sets the default Logger.
Sync syncs all registered loggers.
Trace logs to TRACE log.
TraceContext logs to TRACE log.
TraceContextf logs to TRACE log.
Tracef logs to TRACE log.
Warn logs to WARNING log.
WarnContext logs to WARNING log.
WarnContextf logs to WARNING log.
Warnf logs to WARNING log.
With adds user defined fields to Logger.
WithAdditionalCallerSkip adds additional caller skip.
WithContext add user defined fields to the Logger of context.
WithContextFields sets some user defined data to logs, such as uid, imei, etc.
# Constants
Some ZapCore constants.
Day splits by the day.
Some ZapCore constants.
Hour splits by the hour.
Enums log level constants.
Enums log level constants.
Enums log level constants.
Enums log level constants.
Enums log level constants.
Enums log level constants.
Enums log level constants.
Minute splits by the minute.
Month splits by the month.
output name, default support console and file.
output name, default support console and file.
RollBySize rolls logs by file size.
RollByTime rolls logs by time.
TimeFormatDay is accurate to the day.
TimeFormatHour is accurate to the hour.
TimeFormatMinute is accurate to the minute.
TimeFormatMonth is accurate to the month.
TimeFormatYear is accurate to the year.
WriteAsync writes asynchronously.
WriteFast writes fast(may drop logs asynchronously).
WriteSync writes synchronously.
Year splits by the year.
# Variables
DefaultConsoleWriterFactory is the default console output implementation.
DefaultFileWriterFactory is the default file output implementation.
DefaultLogger the default Logger.
LevelNames is the map from string to log level.
Levels is the map from string to zapcore.Level.
LevelStrings is the map from log level to its string representation.
# Structs
ConsoleWriterFactory is the console writer instance.
Decoder decodes the log.
Field is the user defined log field.
FileWriterFactory is the file writer instance Factory.
FormatConfig is the log format config.
LoggerOptions is the log options.
OutputConfig is the output config, includes console, file and remote.
WriteConfig is the local file config.
# Interfaces
Logger is the underlying logging work for tRPC framework.
OptionLogger defines logger with additional options.
PluginDecoder is the interface used to decode plugin configuration.
No description provided by the author
# Type aliases
Config is the log config.
Level is the log level.
LoggerOption modifies the LoggerOptions.
NewFormatEncoder is the function type for creating a format encoder out of an encoder config.
Option modifies the options of optionLogger.
TimeUnit is the time unit by which files are split, one of minute/hour/day/month/year.
WriteMode is the log write mode, one of 1, 2, 3.