https://github.com/mekramy/gologger
simple high performance Time-Format-Based file logging for golang
https://github.com/mekramy/gologger
go golang log logging mekramy-go
Last synced: 4 months ago
JSON representation
simple high performance Time-Format-Based file logging for golang
- Host: GitHub
- URL: https://github.com/mekramy/gologger
- Owner: mekramy
- License: isc
- Created: 2025-01-26T16:12:58.000Z (5 months ago)
- Default Branch: master
- Last Pushed: 2025-02-12T15:22:29.000Z (4 months ago)
- Last Synced: 2025-02-12T16:28:41.242Z (4 months ago)
- Topics: go, golang, log, logging, mekramy-go
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GoLogger
GoLogger is a flexible and configurable logging library for Go, designed to support both development and production environments. It provides various logging levels, customizable time formats, and file-based logging with different layouts.
## Features
- Multiple logging levels: Debug, Info, Warn, Error, Panic
- Configurable for development and production environments
- Supports standard and Jalaali time formats
- Customizable log file paths, prefixes, extensions, and layouts
- Buffered logging for performance
- Silent mode for disabling logging output## Installation
To install GoLogger, use `go get`:
```sh
go get github.com/mekramy/gologger
```## Usage
### Creating a Logger
You can create a logger using the `LoggerBuilder`:
```go
import "github.com/mekramy/gologger"func main() {
logger, err := gologger.NewLogger().
SetBufferSize(200).
Production().
Simple().
Path("/var/logs").
Prefix("app ").
Extension("log").
Daily().
StdFormatter().
Logger()
if err != nil {
panic(err)
}
defer logger.Sync()logger.Info(gologger.LogOptions{Message: "Application started"})
}
```### Logging Levels
GoLogger supports various logging levels:
```go
logger.Debug(
gologger.With("Name", "John Doe"),
gologger.With("Age", 20),
gologger.WithMessage("Something happend at POST requests!"),
)
logger.Info(...)
logger.Warn(...)
logger.Error(...)
logger.Panic(...)
```## API Documentation
### Logger Interface
The `Logger` interface defines methods for logging at various levels:
- `Debug(options ...LogOptions)`: Creates a new debug level log entry. Intended for development environments.
- `Info(options ...LogOptions)`: Creates a new info level log entry.
- `Warn(options ...LogOptions)`: Creates a new warn level log entry.
- `Error(options ...LogOptions)`: Creates a new error level log entry.
- `Panic(options ...LogOptions)`: Creates a new panic level log entry.
- `Sync()`: Flushes any buffered log entries. Should be called before the application exits to ensure all logs are written.### LoggerBuilder
The `LoggerBuilder` is used to configure and create a `Logger` instance:
- `NewLogger() *LoggerBuilder`: Creates a new `LoggerBuilder` with default settings.
- `SetBufferSize(size uint) *LoggerBuilder`: Sets the buffer size for the logger's queue.
- `Development() *LoggerBuilder`: Sets the logger to development mode.
- `Production() *LoggerBuilder`: Sets the logger to production mode.
- `Simple() *LoggerBuilder`: Sets the logger to use a simple format.
- `Structured() *LoggerBuilder`: Sets the logger to use a structured format.
- `Silent() *LoggerBuilder`: Sets the logger to be silent.
- `Path(root string) *LoggerBuilder`: Sets the root directory for log files.
- `Prefix(prefix string) *LoggerBuilder`: Sets the prefix for log file names.
- `Extension(ext string) *LoggerBuilder`: Sets the extension for log file names.
- `Daily() *LoggerBuilder`: Sets the log file layout to daily.
- `Monthly() *LoggerBuilder`: Sets the log file layout to monthly.
- `CustomLayout(layout string) *LoggerBuilder`: Sets a custom layout for log file names.
- `StdFormatter() *LoggerBuilder`: Sets the logger to use the standard time formatter.
- `JalaaliFormatter() *LoggerBuilder`: Sets the logger to use the Jalaali time formatter.
- `CustomFormatter(formatter TimeFormatter) *LoggerBuilder`: Sets a custom time formatter for the logger.
- `Logger() (Logger, error)`: Creates and returns a `Logger` instance based on the builder's configuration.### TimeFormatter
The `TimeFormatter` function signature is used for custom time formatting:
- `StdFormatter(ts time.Time, layout string) string`: Standard time formatter instance.
- `JalaaliFormatter(ts time.Time, layout string) string`: Jalaali time formatter instance.### LogOptions
The `LogOptions` is option pattern designed for passing parameter to log.
### Example
```go
func main(){
logger.Info(
gologger.With("error", "Something happend!")
)
}
```