-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(log): use
github.com/qdm12/log
- Loading branch information
Showing
17 changed files
with
146 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,73 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/qdm12/golibs/logging" | ||
"github.com/qdm12/golibs/params" | ||
"github.com/qdm12/log" | ||
) | ||
|
||
type Logger struct { | ||
Caller logging.Caller | ||
Level logging.Level | ||
Caller bool | ||
Level log.Level | ||
} | ||
|
||
func (l *Logger) get(env params.Interface) (err error) { | ||
l.Caller, err = env.LogCaller("LOG_CALLER", params.Default("hidden")) | ||
var ( | ||
ErrLogCallerNotValid = errors.New("LOG_CALLER value is not valid") | ||
) | ||
|
||
func readLog() (settings Logger, err error) { | ||
callerString := os.Getenv("LOG_CALLER") | ||
switch callerString { | ||
case "": | ||
case "hidden": | ||
case "short": | ||
settings.Caller = true | ||
default: | ||
return settings, fmt.Errorf("%w: "+ | ||
`%q must be one of "", "hidden" or "short"`, | ||
ErrLogCallerNotValid, callerString) | ||
} | ||
|
||
settings.Level, err = readLogLevel() | ||
if err != nil { | ||
return fmt.Errorf("%w: for environment variable LOG_CALLER", err) | ||
return settings, err | ||
} | ||
|
||
return settings, nil | ||
} | ||
|
||
func readLogLevel() (level log.Level, err error) { | ||
s := os.Getenv("LOG_LEVEL") | ||
if s == "" { | ||
return log.LevelInfo, nil | ||
} | ||
|
||
l.Level, err = env.LogLevel("LOG_LEVEL", params.Default("info")) | ||
level, err = parseLogLevel(s) | ||
if err != nil { | ||
return fmt.Errorf("%w: for environment variable LOG_LEVEL", err) | ||
return level, fmt.Errorf("environment variable LOG_LEVEL: %w", err) | ||
} | ||
|
||
return err | ||
return level, nil | ||
} | ||
|
||
var ErrLogLevelUnknown = errors.New("log level is unknown") | ||
|
||
func parseLogLevel(s string) (level log.Level, err error) { | ||
switch strings.ToLower(s) { | ||
case "debug": | ||
return log.LevelDebug, nil | ||
case "info": | ||
return log.LevelInfo, nil | ||
case "warning": | ||
return log.LevelWarn, nil | ||
case "error": | ||
return log.LevelError, nil | ||
default: | ||
return level, fmt.Errorf( | ||
"%w: %q is not valid and can be one of debug, info, warning or error", | ||
ErrLogLevelUnknown, s) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.