More linting

This commit is contained in:
Paul Tötterman
2021-02-10 16:34:20 +02:00
parent b562bd2dc5
commit ddec572d5d
29 changed files with 275 additions and 107 deletions

View File

@@ -39,8 +39,11 @@ func (h *CompositeMultiHandler) Log(r *Record) (err error) {
// Embed the caller function in the context
if handler != nil {
handler.Log(r)
if err := handler.Log(r); err != nil {
panic(err)
}
}
return
}
@@ -91,8 +94,8 @@ func (h *CompositeMultiHandler) SetHandlers(handler LogHandler, options *LogOpti
}
}
func (h *CompositeMultiHandler) SetJson(writer io.Writer, options *LogOptions) {
handler := CallerFileHandler(StreamHandler(writer, JsonFormatEx(
func (h *CompositeMultiHandler) SetJSON(writer io.Writer, options *LogOptions) {
handler := CallerFileHandler(StreamHandler(writer, JSONFormatEx(
options.GetBoolDefault("pretty", false),
options.GetBoolDefault("lineSeparated", true),
)))
@@ -103,7 +106,7 @@ func (h *CompositeMultiHandler) SetJson(writer io.Writer, options *LogOptions) {
}
// Use built in rolling function.
func (h *CompositeMultiHandler) SetJsonFile(filePath string, options *LogOptions) {
func (h *CompositeMultiHandler) SetJSONFile(filePath string, options *LogOptions) {
writer := &lumberjack.Logger{
Filename: filePath,
MaxSize: options.GetIntDefault("maxSizeMB", 1024), // megabytes
@@ -111,7 +114,7 @@ func (h *CompositeMultiHandler) SetJsonFile(filePath string, options *LogOptions
MaxBackups: options.GetIntDefault("maxBackups", 7),
Compress: options.GetBoolDefault("compress", true),
}
h.SetJson(writer, options)
h.SetJSON(writer, options)
}
func (h *CompositeMultiHandler) SetTerminal(writer io.Writer, options *LogOptions) {

View File

@@ -50,6 +50,7 @@ func CallerFileHandler(h LogHandler) LogHandler {
// Adds in a context called `caller` to the record (contains file name and line number like `foo.go:12`)
// Uses the `log15.CallerFuncHandler` to perform this task.
func CallerFuncHandler(h LogHandler) LogHandler {
// TODO: infinite recursion
return CallerFuncHandler(h)
}
@@ -137,8 +138,9 @@ func NotMatchHandler(key string, value interface{}, h LogHandler) LogHandler {
func MultiHandler(hs ...LogHandler) LogHandler {
return FuncHandler(func(r *Record) error {
for _, h := range hs {
// what to do about failures?
h.Log(r)
if err := h.Log(r); err != nil {
panic(err)
}
}
return nil
})
@@ -189,6 +191,7 @@ func (ll *ListLogHandler) Log(r *Record) (err error) {
handler.Log(r)
}
}
return
}

View File

@@ -13,7 +13,7 @@ import (
func InitializeFromConfig(basePath string, config *config.Context) (c *CompositeMultiHandler) {
// If running in test mode suppress anything that is not an error
if config != nil && config.BoolDefault(TEST_MODE_FLAG, false) {
if config != nil && config.BoolDefault(TestModeFlag, false) {
// Preconfigure all the options
config.SetOption("log.info.output", "none")
config.SetOption("log.debug.output", "none")
@@ -26,14 +26,14 @@ func InitializeFromConfig(basePath string, config *config.Context) (c *Composite
c, _ = NewCompositeMultiHandler()
// Filters are assigned first, non filtered items override filters
if config != nil && !config.BoolDefault(TEST_MODE_FLAG, false) {
if config != nil && !config.BoolDefault(TestModeFlag, false) {
initAllLog(c, basePath, config)
}
initLogLevels(c, basePath, config)
if c.CriticalHandler == nil && c.ErrorHandler != nil {
c.CriticalHandler = c.ErrorHandler
}
if config != nil && !config.BoolDefault(TEST_MODE_FLAG, false) {
if config != nil && !config.BoolDefault(TestModeFlag, false) {
initFilterLog(c, basePath, config)
if c.CriticalHandler == nil && c.ErrorHandler != nil {
c.CriticalHandler = c.ErrorHandler
@@ -47,7 +47,7 @@ func InitializeFromConfig(basePath string, config *config.Context) (c *Composite
// Init the log.all configuration options.
func initAllLog(c *CompositeMultiHandler, basePath string, config *config.Context) {
if config != nil {
extraLogFlag := config.BoolDefault(SPECIAL_USE_FLAG, false)
extraLogFlag := config.BoolDefault(SpecialUseFlag, false)
if output, found := config.String("log.all.output"); found {
// Set all output for the specified handler
if extraLogFlag {
@@ -63,7 +63,7 @@ func initAllLog(c *CompositeMultiHandler, basePath string, config *config.Contex
// log.error.filter ....
func initFilterLog(c *CompositeMultiHandler, basePath string, config *config.Context) {
if config != nil {
extraLogFlag := config.BoolDefault(SPECIAL_USE_FLAG, false)
extraLogFlag := config.BoolDefault(SpecialUseFlag, false)
for _, logFilter := range logFilterList {
// Init for all filters
@@ -102,7 +102,7 @@ func initLogLevels(c *CompositeMultiHandler, basePath string, config *config.Con
"trace", // TODO trace is deprecated
} {
if config != nil {
extraLogFlag := config.BoolDefault(SPECIAL_USE_FLAG, false)
extraLogFlag := config.BoolDefault(SpecialUseFlag, false)
output, found := config.String("log." + name + ".output")
if found {
if extraLogFlag {
@@ -178,7 +178,7 @@ func initHandlerFor(c *CompositeMultiHandler, output, basePath string, options *
}
if strings.HasSuffix(output, "json") {
c.SetJsonFile(output, options)
c.SetJSONFile(output, options)
} else {
// Override defaults for a terminal file
options.SetExtendedOptions("noColor", true)

View File

@@ -59,7 +59,7 @@ var singleCases = []testData{
func TestSingleCases(t *testing.T) {
rootLog := logger.New()
for _, testCase := range singleCases {
testCase.logTest(rootLog, t)
testCase.logTest(t, rootLog)
testCase.validate(t)
}
}
@@ -112,7 +112,7 @@ var filterCases = []testData{
func TestFilterCases(t *testing.T) {
rootLog := logger.New("module", "app")
for _, testCase := range filterCases {
testCase.logTest(rootLog, t)
testCase.logTest(t, rootLog)
testCase.validate(t)
}
}
@@ -169,7 +169,7 @@ var nfilterCases = []testData{
func TestNotFilterCases(t *testing.T) {
rootLog := logger.New("module", "app")
for _, testCase := range nfilterCases {
testCase.logTest(rootLog, t)
testCase.logTest(t, rootLog)
testCase.validate(t)
}
}
@@ -186,7 +186,7 @@ var offCases = []testData{
func TestOffCases(t *testing.T) {
rootLog := logger.New("module", "app")
for _, testCase := range offCases {
testCase.logTest(rootLog, t)
testCase.logTest(t, rootLog)
testCase.validate(t)
}
}
@@ -203,7 +203,7 @@ var duplicateCases = []testData{
func TestDuplicateCases(t *testing.T) {
rootLog := logger.New("module", "app")
for _, testCase := range duplicateCases {
testCase.logTest(rootLog, t)
testCase.logTest(t, rootLog)
testCase.validate(t)
}
}
@@ -232,7 +232,7 @@ var contradictCases = []testData{
func TestContradictCases(t *testing.T) {
rootLog := logger.New("module", "app")
for _, testCase := range contradictCases {
testCase.logTest(rootLog, t)
testCase.logTest(t, rootLog)
testCase.validate(t)
}
}
@@ -253,12 +253,12 @@ var allCases = []testData{
func TestAllCases(t *testing.T) {
rootLog := logger.New("module", "app")
for i, testCase := range allCases {
testCase.logTest(rootLog, t)
testCase.logTest(t, rootLog)
allCases[i] = testCase
}
rootLog = logger.New()
for i, testCase := range allCases {
testCase.logTest(rootLog, t)
testCase.logTest(t, rootLog)
allCases[i] = testCase
}
for _, testCase := range allCases {
@@ -284,7 +284,9 @@ func (c *testCounter) Log(r *logger.Record) error {
return nil
}
func (td *testData) logTest(rootLog logger.MultiLogger, t *testing.T) {
func (td *testData) logTest(t *testing.T, rootLog logger.MultiLogger) {
t.Helper()
if td.tc == nil {
td.tc = &testCounter{}
counterInit(td.tc)
@@ -317,6 +319,8 @@ func (td *testData) runLogTest(log logger.MultiLogger) {
}
func (td *testData) validate(t *testing.T) {
t.Helper()
t.Logf("Test %#v expected %#v", td.tc, td.result)
assert.Equal(t, td.result.debug, td.tc.debug, "Debug failed "+strings.Join(td.config, " "))
assert.Equal(t, td.result.info, td.tc.info, "Info failed "+strings.Join(td.config, " "))

View File

@@ -197,10 +197,10 @@ func escapeString(s string) string {
return ret
}
// 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
// will be logged with a new line between each record.
func JsonFormatEx(pretty, lineSeparated bool) LogFormat {
func JSONFormatEx(pretty, lineSeparated bool) LogFormat {
jsonMarshal := json.Marshal
if pretty {
jsonMarshal = func(v interface{}) ([]byte, error) {

View File

@@ -20,9 +20,9 @@ var (
const (
// The test mode flag overrides the default log level and shows only errors.
TEST_MODE_FLAG = "testModeFlag"
TestModeFlag = "testModeFlag"
// The special use flag enables showing messages when the logger is setup.
SPECIAL_USE_FLAG = "specialUseFlag"
SpecialUseFlag = "specialUseFlag"
)
// Returns the logger for the name.

View File

@@ -9,6 +9,20 @@ import (
"time"
)
// Error is used for constant errors.
type Error string
// Error implements the error interface.
func (e Error) Error() string {
return string(e)
}
const (
ErrNotFunc Error = "not a function"
ErrTakesArgs Error = "takes arguments"
ErrNoReturn Error = "no return value"
)
// Function handler wraps the declared function and returns the handler for it.
func FuncHandler(fn func(r *Record) error) LogHandler {
return funcHandler(fn)
@@ -71,15 +85,15 @@ func evaluateLazy(lz Lazy) (interface{}, error) {
t := reflect.TypeOf(lz.Fn)
if t.Kind() != reflect.Func {
return nil, fmt.Errorf("INVALID_LAZY, not func: %+v", lz.Fn)
return nil, fmt.Errorf("%w %+v", ErrNotFunc, lz.Fn)
}
if t.NumIn() > 0 {
return nil, fmt.Errorf("INVALID_LAZY, func takes args: %+v", lz.Fn)
return nil, fmt.Errorf("%w %+v", ErrTakesArgs, lz.Fn)
}
if t.NumOut() == 0 {
return nil, fmt.Errorf("INVALID_LAZY, no func return val: %+v", lz.Fn)
return nil, fmt.Errorf("%w %+v", ErrNoReturn, lz.Fn)
}
value := reflect.ValueOf(lz.Fn)