Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add external logger support #508

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cron

import (
"io"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -56,6 +57,33 @@ func (pl printfLogger) Error(err error, msg string, keysAndValues ...interface{}
append([]interface{}{msg, "error", err}, keysAndValues...)...)
}

type externalLogger struct {
logger *log.Logger
}

// NewExternalLogger allows for providing an external logging interface through an io.Writer
func NewExternalLogger(w io.Writer) *externalLogger {
l := log.New(w, "cron: ", log.Llongfile)

el := externalLogger{
logger: l,
}

return &el
}

func (el *externalLogger) Info(msg string, keysAndValues ...interface{}) {
el.logger.Printf(
formatString(len(keysAndValues)),
append([]interface{}{msg}, keysAndValues...)...)
}

func (el *externalLogger) Error(err error, msg string, keysAndValues ...interface{}) {
el.logger.Printf(
formatString(len(keysAndValues)+2),
append([]interface{}{msg, "error", err}, keysAndValues...)...)
}

// formatString returns a logfmt-like format string for the number of
// key/values.
func formatString(numKeysAndValues int) string {
Expand Down