mirror of
https://github.com/Rudi9719/loggy.git
synced 2026-03-22 05:17:26 +00:00
Added comments for godoc and exported Logger type
This commit is contained in:
67
loggy.go
67
loggy.go
@ -31,19 +31,28 @@ type Log struct {
|
|||||||
|
|
||||||
// Logging options to be passed to NewLogger()
|
// Logging options to be passed to NewLogger()
|
||||||
type LogOpts struct {
|
type LogOpts struct {
|
||||||
toFile bool
|
// Will set to true if OutFile is set
|
||||||
|
toFile bool
|
||||||
|
// Will set to true if KBTeam is set
|
||||||
toKeybase bool
|
toKeybase bool
|
||||||
toStdout bool
|
// Will set to true if UseStdout is true
|
||||||
OutFile string
|
toStdout bool
|
||||||
KBTeam string
|
// Output file for logging
|
||||||
KBChann string
|
OutFile string
|
||||||
Level LogLevel
|
// Keybase Team for logging
|
||||||
ProgName string
|
KBTeam string
|
||||||
|
// Keybase Channel for logging
|
||||||
|
KBChann string
|
||||||
|
// Log level / verbosity (see LogLevel)
|
||||||
|
Level LogLevel
|
||||||
|
// Program name for Keybase logging
|
||||||
|
ProgName string
|
||||||
|
// Use stdout
|
||||||
UseStdout bool
|
UseStdout bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// A basic Logger with options for logging to file, keybase or stdout
|
// A basic Logger with options for logging to file, keybase or stdout
|
||||||
type logger struct {
|
type Logger struct {
|
||||||
opts LogOpts
|
opts LogOpts
|
||||||
k *keybase.Keybase
|
k *keybase.Keybase
|
||||||
team keybase.Channel
|
team keybase.Channel
|
||||||
@ -68,7 +77,7 @@ func timeStamp() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write log to file
|
// Write log to file
|
||||||
func (l logger) toFile(msg Log) {
|
func (l Logger) toFile(msg Log) {
|
||||||
output := fmt.Sprintf("[%s] %s",
|
output := fmt.Sprintf("[%s] %s",
|
||||||
timeStamp(), msg.String())
|
timeStamp(), msg.String())
|
||||||
|
|
||||||
@ -85,7 +94,7 @@ func (l logger) toFile(msg Log) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send log to Keybase
|
// Send log to Keybase
|
||||||
func (l logger) toKeybase(msg Log) {
|
func (l Logger) toKeybase(msg Log) {
|
||||||
output := fmt.Sprintf("[%s] %s",
|
output := fmt.Sprintf("[%s] %s",
|
||||||
l.opts.ProgName, msg.String())
|
l.opts.ProgName, msg.String())
|
||||||
chat := l.k.NewChat(l.team)
|
chat := l.k.NewChat(l.team)
|
||||||
@ -94,22 +103,22 @@ func (l logger) toKeybase(msg Log) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write log to Stdout
|
// Write log to Stdout
|
||||||
func (l logger) toStdout(msg Log) {
|
func (l Logger) toStdout(msg Log) {
|
||||||
output := fmt.Sprintf("[%s] %s",
|
output := fmt.Sprintf("[%s] %s",
|
||||||
timeStamp(), msg.String())
|
timeStamp(), msg.String())
|
||||||
fmt.Println(output)
|
fmt.Println(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log Info
|
// Log Info shortcut from string
|
||||||
func (l logger) LogInfo(msg string) {
|
func (l Logger) LogInfo(msg string) {
|
||||||
var logMsg Log
|
var logMsg Log
|
||||||
logMsg.Level = Info
|
logMsg.Level = Info
|
||||||
logMsg.Msg = msg
|
logMsg.Msg = msg
|
||||||
handleLog(l, logMsg)
|
handleLog(l, logMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log Debug
|
// Log Debug shortcut from string
|
||||||
func (l logger) LogDebug(msg string) {
|
func (l Logger) LogDebug(msg string) {
|
||||||
var logMsg Log
|
var logMsg Log
|
||||||
logMsg.Level = Debug
|
logMsg.Level = Debug
|
||||||
logMsg.Msg = msg
|
logMsg.Msg = msg
|
||||||
@ -117,24 +126,24 @@ func (l logger) LogDebug(msg string) {
|
|||||||
handleLog(l, logMsg)
|
handleLog(l, logMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log Warning
|
// Log Warning shortcut from string
|
||||||
func (l logger) LogWarn(msg string) {
|
func (l Logger) LogWarn(msg string) {
|
||||||
var logMsg Log
|
var logMsg Log
|
||||||
logMsg.Level = Warnings
|
logMsg.Level = Warnings
|
||||||
logMsg.Msg = msg
|
logMsg.Msg = msg
|
||||||
handleLog(l, logMsg)
|
handleLog(l, logMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log Error
|
// Log Error shortcut from string
|
||||||
func (l logger) LogError(msg string) {
|
func (l Logger) LogError(msg string) {
|
||||||
var logMsg Log
|
var logMsg Log
|
||||||
logMsg.Level = Errors
|
logMsg.Level = Errors
|
||||||
logMsg.Msg = msg
|
logMsg.Msg = msg
|
||||||
handleLog(l, logMsg)
|
handleLog(l, logMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log Critical
|
// Log Critical shortcut from string
|
||||||
func (l logger) LogCritical(msg string) {
|
func (l Logger) LogCritical(msg string) {
|
||||||
var logMsg Log
|
var logMsg Log
|
||||||
logMsg.Level = Critical
|
logMsg.Level = Critical
|
||||||
logMsg.Msg = msg
|
logMsg.Msg = msg
|
||||||
@ -143,15 +152,15 @@ func (l logger) LogCritical(msg string) {
|
|||||||
os.Exit(3)
|
os.Exit(3)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log error type
|
// Log error type for compatibility
|
||||||
func (l logger) LogErrorType(e error) {
|
func (l Logger) LogErrorType(e error) {
|
||||||
var logMsg Log
|
var logMsg Log
|
||||||
logMsg.Level = Critical
|
logMsg.Level = Critical
|
||||||
logMsg.Msg = e.Error()
|
logMsg.Msg = e.Error()
|
||||||
handleLog(l, logMsg)
|
handleLog(l, logMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLog(l logger, logMsg Log) {
|
func handleLog(l Logger, logMsg Log) {
|
||||||
|
|
||||||
if logMsg.Level > l.opts.Level && logMsg.Level != 0 {
|
if logMsg.Level > l.opts.Level && logMsg.Level != 0 {
|
||||||
return
|
return
|
||||||
@ -172,23 +181,25 @@ func handleLog(l logger, logMsg Log) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l logger) Log(level LogLevel, msg string) {
|
// Log func, takes LogLevel and string and passes to internal handler.
|
||||||
|
func (l Logger) Log(level LogLevel, msg string) {
|
||||||
var logMsg Log
|
var logMsg Log
|
||||||
logMsg.Level = level
|
logMsg.Level = level
|
||||||
logMsg.Msg = msg
|
logMsg.Msg = msg
|
||||||
handleLog(l, logMsg)
|
handleLog(l, logMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l logger) LogMsg(msg Log) {
|
// LogMsg takes a type Log and passes it to internal handler.
|
||||||
|
func (l Logger) LogMsg(msg Log) {
|
||||||
handleLog(l, msg)
|
handleLog(l, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new logger instance and pass it
|
// Create a new logger instance and pass it
|
||||||
func NewLogger(opts LogOpts) logger {
|
func NewLogger(opts LogOpts) Logger {
|
||||||
if opts.Level == 0 {
|
if opts.Level == 0 {
|
||||||
opts.Level = 2
|
opts.Level = 2
|
||||||
}
|
}
|
||||||
var l logger
|
var l Logger
|
||||||
if opts.KBTeam != "" {
|
if opts.KBTeam != "" {
|
||||||
l.k = keybase.NewKeybase()
|
l.k = keybase.NewKeybase()
|
||||||
var chann keybase.Channel
|
var chann keybase.Channel
|
||||||
|
|||||||
Reference in New Issue
Block a user