log: fixes for golint warnings (#16775)

This commit is contained in:
kiel barry 2018-05-22 00:28:43 -07:00 committed by Péter Szilágyi
parent 0fe47e98c4
commit 09d44247f7
5 changed files with 20 additions and 19 deletions

View File

@ -45,7 +45,7 @@ srvlog.SetHandler(log.MultiHandler(
log.StreamHandler(os.Stderr, log.LogfmtFormat()), log.StreamHandler(os.Stderr, log.LogfmtFormat()),
log.LvlFilterHandler( log.LvlFilterHandler(
log.LvlError, log.LvlError,
log.Must.FileHandler("errors.json", log.JsonFormat())))) log.Must.FileHandler("errors.json", log.JSONFormat()))))
``` ```
Will result in output that looks like this: Will result in output that looks like this:

View File

@ -86,7 +86,7 @@ from the rpc package in logfmt to standard out. The other prints records at Erro
or above in JSON formatted output to the file /var/log/service.json or above in JSON formatted output to the file /var/log/service.json
handler := log.MultiHandler( handler := log.MultiHandler(
log.LvlFilterHandler(log.LvlError, log.Must.FileHandler("/var/log/service.json", log.JsonFormat())), log.LvlFilterHandler(log.LvlError, log.Must.FileHandler("/var/log/service.json", log.JSONFormat())),
log.MatchFilterHandler("pkg", "app/rpc" log.StdoutHandler()) log.MatchFilterHandler("pkg", "app/rpc" log.StdoutHandler())
) )
@ -304,8 +304,8 @@ For all Handler functions which can return an error, there is a version of that
function which will return no error but panics on failure. They are all available function which will return no error but panics on failure. They are all available
on the Must object. For example: on the Must object. For example:
log.Must.FileHandler("/path", log.JsonFormat) log.Must.FileHandler("/path", log.JSONFormat)
log.Must.NetHandler("tcp", ":1234", log.JsonFormat) log.Must.NetHandler("tcp", ":1234", log.JSONFormat)
Inspiration and Credit Inspiration and Credit

View File

@ -196,16 +196,16 @@ func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) {
buf.WriteByte('\n') buf.WriteByte('\n')
} }
// JsonFormat formats log records as JSON objects separated by newlines. // JSONFormat formats log records as JSON objects separated by newlines.
// It is the equivalent of JsonFormatEx(false, true). // It is the equivalent of JSONFormatEx(false, true).
func JsonFormat() Format { func JSONFormat() Format {
return JsonFormatEx(false, true) return JSONFormatEx(false, true)
} }
// JsonFormatEx formats log records as JSON objects. If pretty is true, // JSONFormatEx formats log records as JSON objects. If pretty is true,
// records will be pretty-printed. If lineSeparated is true, records // records will be pretty-printed. If lineSeparated is true, records
// will be logged with a new line between each record. // will be logged with a new line between each record.
func JsonFormatEx(pretty, lineSeparated bool) Format { func JSONFormatEx(pretty, lineSeparated bool) Format {
jsonMarshal := json.Marshal jsonMarshal := json.Marshal
if pretty { if pretty {
jsonMarshal = func(v interface{}) ([]byte, error) { jsonMarshal = func(v interface{}) ([]byte, error) {
@ -225,7 +225,7 @@ func JsonFormatEx(pretty, lineSeparated bool) Format {
if !ok { if !ok {
props[errorKey] = fmt.Sprintf("%+v is not a string key", r.Ctx[i]) props[errorKey] = fmt.Sprintf("%+v is not a string key", r.Ctx[i])
} }
props[k] = formatJsonValue(r.Ctx[i+1]) props[k] = formatJSONValue(r.Ctx[i+1])
} }
b, err := jsonMarshal(props) b, err := jsonMarshal(props)
@ -270,7 +270,7 @@ func formatShared(value interface{}) (result interface{}) {
} }
} }
func formatJsonValue(value interface{}) interface{} { func formatJSONValue(value interface{}) interface{} {
value = formatShared(value) value = formatShared(value)
switch value.(type) { switch value.(type) {
case int, int8, int16, int32, int64, float32, float64, uint, uint8, uint16, uint32, uint64, string: case int, int8, int16, int32, int64, float32, float64, uint, uint8, uint16, uint32, uint64, string:

View File

@ -11,8 +11,8 @@ import (
"github.com/go-stack/stack" "github.com/go-stack/stack"
) )
// Handler defines where and how log records are written.
// A Logger prints its log records by writing to a Handler. // A Logger prints its log records by writing to a Handler.
// The Handler interface defines where and how log records are written.
// Handlers are composable, providing you great flexibility in combining // Handlers are composable, providing you great flexibility in combining
// them to achieve the logging structure that suits your applications. // them to achieve the logging structure that suits your applications.
type Handler interface { type Handler interface {
@ -193,7 +193,7 @@ func LvlFilterHandler(maxLvl Lvl, h Handler) Handler {
}, h) }, h)
} }
// A MultiHandler dispatches any write to each of its handlers. // MultiHandler dispatches any write to each of its handlers.
// This is useful for writing different types of log information // This is useful for writing different types of log information
// to different locations. For example, to log to a file and // to different locations. For example, to log to a file and
// standard error: // standard error:
@ -212,7 +212,7 @@ func MultiHandler(hs ...Handler) Handler {
}) })
} }
// A FailoverHandler writes all log records to the first handler // FailoverHandler writes all log records to the first handler
// specified, but will failover and write to the second handler if // specified, but will failover and write to the second handler if
// the first handler has failed, and so on for all handlers specified. // the first handler has failed, and so on for all handlers specified.
// For example you might want to log to a network socket, but failover // For example you might want to log to a network socket, but failover
@ -220,7 +220,7 @@ func MultiHandler(hs ...Handler) Handler {
// standard out if the file write fails: // standard out if the file write fails:
// //
// log.FailoverHandler( // log.FailoverHandler(
// log.Must.NetHandler("tcp", ":9090", log.JsonFormat()), // log.Must.NetHandler("tcp", ":9090", log.JSONFormat()),
// log.Must.FileHandler("/var/log/app.log", log.LogfmtFormat()), // log.Must.FileHandler("/var/log/app.log", log.LogfmtFormat()),
// log.StdoutHandler) // log.StdoutHandler)
// //
@ -336,7 +336,7 @@ func DiscardHandler() Handler {
}) })
} }
// The Must object provides the following Handler creation functions // Must provides the following Handler creation functions
// which instead of returning an error parameter only return a Handler // which instead of returning an error parameter only return a Handler
// and panic on failure: FileHandler, NetHandler, SyslogHandler, SyslogNetHandler // and panic on failure: FileHandler, NetHandler, SyslogHandler, SyslogNetHandler
var Must muster var Must muster

View File

@ -24,7 +24,7 @@ const (
LvlTrace LvlTrace
) )
// Aligned returns a 5-character string containing the name of a Lvl. // AlignedString returns a 5-character string containing the name of a Lvl.
func (l Lvl) AlignedString() string { func (l Lvl) AlignedString() string {
switch l { switch l {
case LvlTrace: case LvlTrace:
@ -64,7 +64,7 @@ func (l Lvl) String() string {
} }
} }
// Returns the appropriate Lvl from a string name. // LvlFromString returns the appropriate Lvl from a string name.
// Useful for parsing command line args and configuration files. // Useful for parsing command line args and configuration files.
func LvlFromString(lvlString string) (Lvl, error) { func LvlFromString(lvlString string) (Lvl, error) {
switch lvlString { switch lvlString {
@ -95,6 +95,7 @@ type Record struct {
KeyNames RecordKeyNames KeyNames RecordKeyNames
} }
// RecordKeyNames gets stored in a Record when the write function is executed.
type RecordKeyNames struct { type RecordKeyNames struct {
Time string Time string
Msg string Msg string