Documentation ¶
Overview ¶
This file manages everything related slog handler that's to be used by bark client.
Index ¶
- Constants
- Variables
- func Get(url string) (string, appError.AppErr)
- func InsertSingleRequest(logEntry models.BarkLog)
- func PostLog(url string, log models.BarkLog) (string, appError.AppErr)
- func PostLogArray(url string, log []models.BarkLog) (string, appError.AppErr)
- func SlogHandlerOptions() *slog.HandlerOptions
- type BarkSlogHandler
- func (handle *BarkSlogHandler) Enabled(_ context.Context, level slog.Level) bool
- func (handle *BarkSlogHandler) Handle(ctx context.Context, record slog.Record) error
- func (handle *BarkSlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (handle *BarkSlogHandler) WithGroup(name string) slog.Handler
- type Config
- func NewClient(url, defaultLogLvl, svcName, svcInstName string, enableSlog bool, ...) *Config
- func NewClientWithServer(dbUrl, defaultLogLvl, svcName, svcInstName string, enableSlog bool) *Config
- func NewSloggerClient(defaultLogLevel string) *Config
- func NewSloggerClientJson(defaultLogLvl string) *Config
- func (c *Config) Alert(message string, blocking bool)
- func (c *Config) Alertf(message string, blocking bool, format ...any)
- func (c *Config) Debug(message string)
- func (c *Config) Debugf(message string, format ...any)
- func (c *Config) Default(message string)
- func (c *Config) DisableDebugLogs()
- func (c *Config) EnableDebugLogs()
- func (c *Config) Error(message string)
- func (c *Config) Errorf(message string, format ...any)
- func (c *Config) Info(message string)
- func (c *Config) Infof(message string, format ...any)
- func (c *Config) Notice(message string)
- func (c *Config) Noticef(message string, format ...any)
- func (c *Config) Panic(message string)
- func (c *Config) Panicf(message string, format ...any)
- func (c *Config) Printf(message string, format ...any)
- func (c *Config) Println(message string)
- func (c *Config) Raw(rawLog RawLog, returnError bool) error
- func (c *Config) SetAlertWebhook(f webhook)
- func (c *Config) SetCustomOut(out io.Writer)
- func (c *Config) SetSlogHandler(handler slog.Handler)
- func (c *Config) WaitAndEnd()
- func (c *Config) Warn(message string)
- func (c *Config) Warnf(message string, format ...any)
- type RawLog
Constants ¶
const ( LvlPanic = slog.Level(10) LvlAlert = slog.Level(9) LvlError = slog.Level(8) LvlWarning = slog.Level(4) LvlNotice = slog.Level(3) LvlInfo = slog.Level(0) LvlDebug = slog.Level(-4) )
Constants for custom log levels in bark.
const ( PANIC = "PANIC" ALERT = "ALERT" ERROR = "ERROR" WARNING = "WARN" NOTICE = "NOTICE" INFO = "INFO" DEBUG = "DEBUG" )
const ChannelCapacity = 100000
Variables ¶
var PendingLogsChan chan models.BarkLog
var Wg sync.WaitGroup
Functions ¶
func Get ¶ added in v1.1.0
Get makes a HTTP get request on the url and returns a string response back.
func InsertSingleRequest ¶ added in v1.1.0
InsertSingleRequest sends logEntry to PendingLogChan
func PostLog ¶ added in v1.1.0
PostLog makes a HTTP post request to bark server url and send BarkLog as payload.
func PostLogArray ¶ added in v1.1.0
PostLogArray makes a HTTP post request to bark server url and sends an array of BarkLog as payload.
func SlogHandlerOptions ¶ added in v1.1.0
func SlogHandlerOptions() *slog.HandlerOptions
SlogHandlerOptions returns slog.HandlerOptions which defines custom log levels.
Types ¶
type BarkSlogHandler ¶ added in v1.1.0
type BarkSlogHandler struct {
// contains filtered or unexported fields
}
BarkSlogHandler implements interface slog.Handler.
func NewBarkSlogHandler ¶ added in v1.1.0
func NewBarkSlogHandler(out io.Writer) *BarkSlogHandler
NewBarkSlogHandler returns an object of BarkSlogHandler
func (*BarkSlogHandler) Enabled ¶ added in v1.1.0
Enabled method is an implementation of slog.Handler interface's method for BarkSlogHandler This method defines which log levels are supported.
func (*BarkSlogHandler) Handle ¶ added in v1.1.0
Handle is an implementation of slog.Handler interface's methods for BarkSlogHandler.
func (*BarkSlogHandler) WithAttrs ¶ added in v1.1.0
func (handle *BarkSlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler
WithAttrs method is an implementation of slog.Handler interface's method for BarkSlogHandler This allows a set of attributes to be added to slog package, but right now we're not supporting additional slog attributes. This method returns the handler as is for now.
func (*BarkSlogHandler) WithGroup ¶ added in v1.1.0
func (handle *BarkSlogHandler) WithGroup(name string) slog.Handler
WithGroup method is an implementation of slog.Handler interface's method for BarkSlogHandler This allows a group to be added to slog package, but right now we're not supporting slog groups. This method returns the handler as is for now.
type Config ¶
type Config struct { BaseUrl string ErrorLevel string ServiceName string ServiceInstanceName string BulkSend bool Slogger *slog.Logger AlertWebhook webhook // contains filtered or unexported fields }
func NewClient ¶
func NewClient(url, defaultLogLvl, svcName, svcInstName string, enableSlog bool, enableBulkSend bool) *Config
NewClient creates and returns a new Config object with the given parameters. A Config object represents the configuration for logging to a remote server. Config object is the main point interactions between user and bark client library.
The url parameter is the base URL of the remote bark server where the logs will be sent. It must be a valid URL string and must end in `/`
The defaultLogLvl parameter is the log level for logging. It must be one of the constants defined in the constants package, such as INFO, WARN, ERROR, etc. If an invalid value is given, the function will print a warning message and use INFO as the default level.
The svcName parameter is the name of the service that is logging. It must be a non-empty string. If an empty string is given, the function will print a warning message and use constants.DefaultLogServiceName as the default value.
The svcInstName parameter is the name of the service instance that is logging. It must be a non-empty string. If an empty string is given, the function will print a warning message and use appRuntime.SessionName as the default value.
The enableSlog parameter is a boolean flag that indicates whether to enable slog logging to standard output. If true, the function will create and assign a new slog.Logger object to the Config object. If false, the Config object will have a nil Slogger field.
The enableBulkSend parameter is a boolean flag that indicates whether to enable bulk sending of logs to the remote server. If true, the function will start a goroutine that periodically sends all the buffered logs to the server. If false, the logs will be sent individually as they are generated.
func NewClientWithServer ¶ added in v1.1.0
func NewClientWithServer(dbUrl, defaultLogLvl, svcName, svcInstName string, enableSlog bool) *Config
NewClientWithServer returns a client config which performs the job of the server as well It differs from NewClient in two main ways: it does not have the option to do bulk inserts (they are not needed) and it accepts the database URL instead of server URL.
The url parameter is the database URL where the logs will be stored. It must be a valid postgresql protocol string.
The defaultLogLvl parameter is the log level for logging. It must be one of the constants defined in the constants package, such as INFO, WARN, ERROR, etc. If an invalid value is given, the function will print a warning message and use INFO as the default level.
The svcName parameter is the name of the service that is logging. It must be a non-empty string. If an empty string is given, the function will print a warning message and use constants.DefaultLogServiceName as the default value.
The svcInstName parameter is the name of the service instance that is logging. It must be a non-empty string. If an empty string is given, the function will print a warning message and use appRuntime.SessionName as the default value.
The enableSlog parameter is a boolean flag that indicates whether to enable slog logging to standard output. If true, the function will create and assign a new slog.Logger object to the Config object. If false, the Config object will have a nil Slogger field.
func NewSloggerClient ¶ added in v1.1.0
NewSloggerClient creates and returns a new Config object which can be used for fully client-side logging only It accepts one single parameter - the default log level. The config returned cannot be used to send logs to any remote server. This can be used for projects that do not aim to send logs to a remote service yet!
func NewSloggerClientJson ¶ added in v1.1.1
NewSloggerClientJson returns a Config object that will print logs to stdout in JSON format. *Config returned by NewSloggerClientJson behaves the same as NewSloggerClient, but it prints the logs in JSON. NewClientWithJSONSlogger accepts the same parameters as NewSloggerClient.
func (*Config) Alert ¶
Alert sends a LvlAlert log to server and prints the log if slog is enabled. This also initiates a AlertWebhook call.
func (*Config) Alertf ¶
Alertf performs the same operation as Config.Alert but it accepts a format specifier.
func (*Config) Debug ¶
Debug sends a LvlDebug level log to server and prints the log if slog is enabled.
func (*Config) Debugf ¶
Debugf performs the same operation as Config.Debug but it accepts a format specifier.
func (*Config) Default ¶ added in v1.1.0
Default sends a Config.Default level log to server (specified while creating client) and prints the log if slog is enabled.
func (*Config) DisableDebugLogs ¶ added in v1.1.0
func (c *Config) DisableDebugLogs()
func (*Config) EnableDebugLogs ¶ added in v1.1.0
func (c *Config) EnableDebugLogs()
func (*Config) Error ¶
Error sends a LvlError level log to server and prints the log if slog is enabled.
func (*Config) Errorf ¶
Errorf performs the same operation as Config.Error but it accepts a format specifier.
func (*Config) Info ¶
Info sends a LvlInfo level log to server and prints the log if slog is enabled.
func (*Config) Infof ¶
Infof performs the same operation as Config.Info but it accepts a format specifier.
func (*Config) Notice ¶
Notice sends a LvlNotice level log to server and prints the log if slog is enabled.
func (*Config) Noticef ¶
Noticef performs the same operation as Config.Notice but it accepts a format specifier.
func (*Config) Panicf ¶
Panicf performs the same operation as Config.Panic but it accepts a format specifier.
func (*Config) Printf ¶
Printf performs the same operation as Config.Println but it accepts a format specifier.
func (*Config) Println ¶
Println sends logs to server based on LMID passed in message string. If the LMID is invalid an LvlInfo log level considered by default. This method prints the logs regardless if the slog is enabled or not.
func (*Config) SetAlertWebhook ¶ added in v1.1.0
func (c *Config) SetAlertWebhook(f webhook)
SetAlertWebhook sets the function f to be used as a webhook to be used by Config.Alert method
func (*Config) SetCustomOut ¶ added in v1.1.0
SetCustomOut allows users to set output to custom writer instead of the default standard output
func (*Config) SetSlogHandler ¶ added in v1.1.0
SetSlogHandler allows users to specify their own slog handler
func (*Config) WaitAndEnd ¶ added in v1.1.0
func (c *Config) WaitAndEnd()
WaitAndEnd will wait for all logs to be sent to server. This is an optional blocking call if the there are unsent logs.