-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package pprof | ||
|
||
import ( | ||
"context" | ||
"github.com/unpackdev/unpack/pkg/options" | ||
"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) | ||
} |