amebo

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2025 License: MIT Imports: 8 Imported by: 0

README

Amebo

Go Version

A flexible, structured logging library for Go applications built on top of zap with simplified configuration and multiple output options.

Features

  • Simple yet powerful logging interface
  • Support for both console and file-based logging
  • Multiple output formats (JSON/text)
  • Log rotation for file-based logging
  • Configurable log levels
  • Built-in mock for testing
  • Fluent API for configuration

Installation

go get github.com/fleetcontrolsio/amebo

Quick Start

package main

import (
	"github.com/fleetcontrolsio/amebo"
	"go.uber.org/zap/zapcore"
)

func main() {
	// Create logger with default console output
	options := amebo.NewLoggerOptions().
		SetLogLevel("INFO").
		SetLogFormat("json")

	logger, err := amebo.NewLogger(options)
	if err != nil {
		panic(err)
	}

	// Log messages
	logger.Info("Application started")
	logger.Debug("Debug message", zapcore.Field{Key: "value", Type: zapcore.StringType, String: "test"})
	logger.Error("Something went wrong", zapcore.Field{Key: "error_code", Type: zapcore.Int64Type, Integer: 500})
}

Configuration Options

Console Logging
options := amebo.NewLoggerOptions().
	SetLogLevel("INFO").      // DEBUG, INFO, WARN, ERROR, FATAL
	SetLogOutput("console").  // Output to stdout
	SetLogFormat("json")      // json or text format
File Logging
options := amebo.NewLoggerOptions().
	SetLogLevel("INFO").
	SetLogOutput("file").
	SetLogFormat("json").
	SetLogDirectory("logs").
	SetLogFilePrefix("myapp").
	SetMaxFileSize(100)  // In MB

Log Levels

  • DEBUG: Detailed information, typically useful for debugging
  • INFO: General operational information
  • WARN: Warning events that might cause issues
  • ERROR: Error events that might still allow the application to continue running
  • FATAL: Severe error events that cause the application to terminate

Testing with Mock Logger

import (
	"testing"
	
	"github.com/fleetcontrolsio/amebo"
	"github.com/stretchr/testify/mock"
	"go.uber.org/zap/zapcore"
)

func TestWithLogger(t *testing.T) {
	mockLogger := amebo.NewMockLogger()
	
	// Set expectations
	mockLogger.On("Info", "test message", mock.Anything).Return()
	
	// Use mock logger in your code
	mockLogger.Info("test message", zapcore.Field{Key: "test", Type: zapcore.StringType, String: "value"})
	
	// Verify expectations were met
	mockLogger.AssertExpectations(t)
}

License

MIT

Dependencies

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFileLogger

func NewFileLogger(opts *FileLoggerOptions) (*lumberjack.Logger, error)

func ParseLogLevel

func ParseLogLevel(level string) zap.AtomicLevel

Types

type FileLoggerOptions

type FileLoggerOptions struct {
	LogDirectory  string
	LogFilePrefix string
	MaxSize       int
	MaxAge        int
}

type Logger

type Logger interface {
	Debug(msg string, fields ...zapcore.Field)
	Info(msg string, fields ...zapcore.Field)
	Warn(msg string, fields ...zapcore.Field)
	Error(msg string, fields ...zapcore.Field)
	Fatal(msg string, fields ...zapcore.Field)
}

func NewLogger

func NewLogger(options *LoggerOptions) (Logger, error)

NewLogger creates a new logger based on the provided options

func NewZapLogger

func NewZapLogger(opts *LoggerOptions) (Logger, error)

Create a new zap logger

type LoggerOptions

type LoggerOptions struct {
	// Log level: debug, info, warn, error, fatal
	LogLevel string
	// Where to output logs; console, file
	LogOutput string
	// Log output format: text, json
	LogFormat string
	// Log file prefix (only used when LogOutput is file)
	LogFilePrefix string
	// Log directory (only used when LogOutput is file)
	LogDirectory string
	// Max file size in MB (only used when LogOutput is file)
	MaxFileSize int
}

func NewLoggerOptions

func NewLoggerOptions() *LoggerOptions

func (*LoggerOptions) SetLogDirectory

func (o *LoggerOptions) SetLogDirectory(directory string) *LoggerOptions

func (*LoggerOptions) SetLogFilePrefix

func (o *LoggerOptions) SetLogFilePrefix(prefix string) *LoggerOptions

func (*LoggerOptions) SetLogFormat

func (o *LoggerOptions) SetLogFormat(format string) *LoggerOptions

func (*LoggerOptions) SetLogLevel

func (o *LoggerOptions) SetLogLevel(level string) *LoggerOptions

func (*LoggerOptions) SetLogOutput

func (o *LoggerOptions) SetLogOutput(output string) *LoggerOptions

func (*LoggerOptions) SetMaxFileSize

func (o *LoggerOptions) SetMaxFileSize(size int) *LoggerOptions

func (*LoggerOptions) Validate

func (o *LoggerOptions) Validate() error

type MockLogger

type MockLogger struct {
	mock.Mock
}

MockLogger implements the Logger interface for testing

func NewMockLogger

func NewMockLogger() *MockLogger

NewMockLogger creates a new mock logger

func (*MockLogger) Debug

func (m *MockLogger) Debug(msg string, fields ...zapcore.Field)

Debug logs a debug level message

func (*MockLogger) Error

func (m *MockLogger) Error(msg string, fields ...zapcore.Field)

Error logs an error level message

func (*MockLogger) Fatal

func (m *MockLogger) Fatal(msg string, fields ...zapcore.Field)

Fatal logs a fatal level message

func (*MockLogger) Info

func (m *MockLogger) Info(msg string, fields ...zapcore.Field)

Info logs an info level message

func (*MockLogger) Warn

func (m *MockLogger) Warn(msg string, fields ...zapcore.Field)

Warn logs a warning level message

Jump to

Keyboard shortcuts

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