Skip to content

Commit

Permalink
logger and pprof packages
Browse files Browse the repository at this point in the history
  • Loading branch information
0x19 committed Sep 15, 2024
1 parent 6b38be9 commit b2c6d36
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 0 deletions.
12 changes: 12 additions & 0 deletions logger/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Package logger provides a simplified interface for creating and configuring loggers
// using the Uber's zap logging library. It includes functions to create both production
// and development loggers with appropriate configurations.
//
// The production logger is optimized for performance and is suitable for use in
// a production environment. The development logger, on the other hand, provides
// more verbose output and is intended for use during the development process.
//
// Additionally, provides a simple and convenient wrapper around the zap logging
// library. It offers structured logging capabilities across various log levels,
// including fatal, error, warning, info, and debug.
package logger
68 changes: 68 additions & 0 deletions logger/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package logger

import "go.uber.org/zap"

// Fatal logs a message at the fatal level including any additional fields provided.
// The logger then calls os.Exit(1), terminating the program.
// Use this level for errors that should not occur during normal operation and indicate
// a severe problem that requires immediate attention.
//
// Parameters:
//
// message - The log message to be written.
// err - The error object associated with this log message.
// fields - Optional zap fields for additional structured context.
func Fatal(message string, err error, fields ...zap.Field) {
zap.L().Fatal(message, append(fields, zap.Error(err))...)
}

// Error logs a message at the error level including any additional fields provided.
// This level is used for logging errors that have occurred during execution.
// These errors might require attention but do not necessarily indicate an immediate
// failure of the entire application.
//
// Parameters:
//
// message - The log message to be written.
// err - The error object associated with this log message.
// fields - Optional zap fields for additional structured context.
func Error(message string, err error, fields ...zap.Field) {
zap.L().Error(message, append(fields, zap.Error(err))...)
}

// Warn logs a message at the warning level including any additional fields provided.
// This level is used for potentially harmful situations that warrant attention
// but do not represent immediate errors.
//
// Parameters:
//
// message - The log message to be written.
// fields - Optional zap fields for additional structured context.
func Warn(message string, fields ...zap.Field) {
zap.L().Warn(message, fields...)
}

// Info logs a message at the info level including any additional fields provided.
// Use this level for informational messages that highlight the progress of the application
// under normal circumstances.
//
// Parameters:
//
// message - The log message to be written.
// fields - Optional zap fields for additional structured context.
func Info(message string, fields ...zap.Field) {
zap.L().Info(message, fields...)
}

// Debug logs a message at the debug level including any additional fields provided.
// This level is used for detailed informational messages that are useful for debugging
// an application. These messages are typically voluminous and are not required in
// a production environment.
//
// Parameters:
//
// message - The log message to be written.
// fields - Optional zap fields for additional structured context.
func Debug(message string, fields ...zap.Field) {
zap.L().Debug(message, fields...)
}
62 changes: 62 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package logger

import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// GetProductionLogger creates and returns a new zap.Logger configured for production use.
// The production logger is optimized for performance. It uses a JSON encoder, logs to standard
// error, and writes at InfoLevel and above.
//
// Returns:
//
// *zap.Logger - The configured zap.Logger for production use.
// error - An error if the logger could not be created.
func GetProductionLogger(level zap.AtomicLevel) (*zap.Logger, error) {
config := zap.NewProductionConfig()
config.Level = level
logger, err := config.Build()
return logger, err
}

// GetDevelopmentLogger creates and returns a new zap.Logger configured for development use.
// The development logger is more verbose and is intended for use during development. It uses
// a console encoder with colored level output and logs at the specified log level.
//
// Parameters:
//
// level - The minimum logging level at which logs should be written,
// e.g., zapcore.DebugLevel, zapcore.InfoLevel.
//
// Returns:
//
// *zap.Logger - The configured zap.Logger for development use.
// error - An error if the logger could not be created.
func GetDevelopmentLogger(level zap.AtomicLevel) (*zap.Logger, error) {
config := zap.NewDevelopmentConfig()
config.Level = level
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
logger, err := config.Build()
return logger, err
}

func GetLogger(env string, level string) (*zap.Logger, error) {
configLevel, err := zap.ParseAtomicLevel(level)
if err != nil {
return nil, fmt.Errorf(
"invalid logger level provided: %s - err: %s",
level, err,
)
}

switch env {
case "development":
return GetDevelopmentLogger(configLevel)
case "production":
return GetProductionLogger(configLevel)
default:
return nil, fmt.Errorf("failure to construct logger for env: %s", env)
}
}
2 changes: 2 additions & 0 deletions pprof/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package pprof defines a utility for integrating pprof with custom configurations.
package pprof
45 changes: 45 additions & 0 deletions pprof/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package pprof

import (
"context"
"github.com/unpackdev/unpack/pkg/options"

Check failure on line 5 in pprof/pprof.go

View workflow job for this annotation

GitHub Actions / test (1.22)

no required module provides package github.com/unpackdev/unpack/pkg/options; to add it:
"go.uber.org/zap"
"net/http"
_ "net/http/pprof"
)

// Pprof encapsulates the pprof server configuration.
type Pprof struct {
ctx context.Context
opts options.Pprof
}

// New creates a new Pprof instance with the specified listen address.
func New(ctx context.Context, opts options.Pprof) *Pprof {
return &Pprof{ctx: ctx, opts: opts}
}

// IsEnabled returns if pprof server is enabled or not (via configuration)
func (p *Pprof) IsEnabled() bool {
return p.opts.Enabled
}

// GetName returns options service name associated with pprof server
func (p *Pprof) GetName() string {
return p.opts.Name
}

// GetAddress returns the address on which pprof server will be listening on
func (p *Pprof) GetAddress() string {
return p.opts.Addr
}

// Start initializes the pprof HTTP server on the configured address.
func (p *Pprof) Start() error {
zap.L().Info(
"Started up pprof server",
zap.String("service", p.opts.Name),
zap.String("address", p.opts.Addr),
)
return http.ListenAndServe(p.opts.Addr, nil)
}

0 comments on commit b2c6d36

Please sign in to comment.