parent
6b10d2b842
commit
84f2a97060
27
logger.go
27
logger.go
|
@ -26,6 +26,10 @@ type Logger struct {
|
||||||
// Whether to print the file and line number of the invoking (calling line)
|
// Whether to print the file and line number of the invoking (calling line)
|
||||||
PrintSource bool
|
PrintSource bool
|
||||||
|
|
||||||
|
// Only print the log message without any additional info. This property will ignore other options linke
|
||||||
|
// PrintSource or FuncCallIncrement
|
||||||
|
OnlyPrintMessage bool
|
||||||
|
|
||||||
// While logging, the file and line number of the
|
// While logging, the file and line number of the
|
||||||
// invoking (calling) line can be printed out.
|
// invoking (calling) line can be printed out.
|
||||||
// This defines an offset that is applied to the call stack.
|
// This defines an offset that is applied to the call stack.
|
||||||
|
@ -96,14 +100,22 @@ func (l *Logger) log(level Level, message string, parameters ...any) {
|
||||||
// Get the name of the level to log
|
// Get the name of the level to log
|
||||||
var levelName = fmt.Sprintf("%-5s", level)
|
var levelName = fmt.Sprintf("%-5s", level)
|
||||||
|
|
||||||
printMessage := "[" + levelName + "] " + time.Now().Local().Format("2006-01-02 15:04:05") +
|
// Build the message to print
|
||||||
getSourceMessage(file, line, pc, l) + " - " + fmt.Sprintf(message, parameters...)
|
printMessage := fmt.Sprintf(message, parameters...)
|
||||||
|
if !l.OnlyPrintMessage {
|
||||||
|
printMessage = "[" + levelName + "] " + time.Now().Local().Format("2006-01-02 15:04:05") +
|
||||||
|
getSourceMessage(file, line, pc, l) + " - " + printMessage
|
||||||
|
}
|
||||||
|
|
||||||
printMessageColored :=
|
// Build the colored message to print
|
||||||
l.getColored("["+levelName+"] ", level.getColor()) +
|
printMessageColored := l.getColored(fmt.Sprintf(message, parameters...), level.getColor())
|
||||||
l.getColored(time.Now().Local().Format("2006-01-02 15:04:05"), colCyan) +
|
if !l.OnlyPrintMessage {
|
||||||
l.getColored(getSourceMessage(file, line, pc, l), colPurple) + " - " +
|
printMessageColored =
|
||||||
l.getColored(fmt.Sprintf(message, parameters...), level.getColor())
|
l.getColored("["+levelName+"] ", level.getColor()) +
|
||||||
|
l.getColored(time.Now().Local().Format("2006-01-02 15:04:05"), colCyan) +
|
||||||
|
l.getColored(getSourceMessage(file, line, pc, l), colPurple) + " - " +
|
||||||
|
printMessageColored
|
||||||
|
}
|
||||||
|
|
||||||
if l.File.Level <= level && l.File.logger != nil {
|
if l.File.Level <= level && l.File.logger != nil {
|
||||||
l.File.writeToFile(printMessage, level)
|
l.File.writeToFile(printMessage, level)
|
||||||
|
@ -235,6 +247,7 @@ func CloseFile() {
|
||||||
func GetLoggerFromEnv(defaultLogger *Logger) *Logger {
|
func GetLoggerFromEnv(defaultLogger *Logger) *Logger {
|
||||||
defaultLogger.ColoredOutput = getEnvBool("LOGGER_COLOREDOUTPUT", defaultLogger.ColoredOutput)
|
defaultLogger.ColoredOutput = getEnvBool("LOGGER_COLOREDOUTPUT", defaultLogger.ColoredOutput)
|
||||||
defaultLogger.Level = GetLevelByName(getEnvString("LOGGER_LEVEL", defaultLogger.Level.String()))
|
defaultLogger.Level = GetLevelByName(getEnvString("LOGGER_LEVEL", defaultLogger.Level.String()))
|
||||||
|
defaultLogger.OnlyPrintMessage = getEnvBool("LOGGER_ONLYPRINTMESSAGE", defaultLogger.OnlyPrintMessage)
|
||||||
defaultLogger.File.Level = GetLevelByName(getEnvString("LOGGER_FILE_LEVEL", defaultLogger.File.Level.String()))
|
defaultLogger.File.Level = GetLevelByName(getEnvString("LOGGER_FILE_LEVEL", defaultLogger.File.Level.String()))
|
||||||
defaultLogger.File.Path = getEnvString("LOGGER_FILE_PATH", defaultLogger.File.Path)
|
defaultLogger.File.Path = getEnvString("LOGGER_FILE_PATH", defaultLogger.File.Path)
|
||||||
defaultLogger.File.AppendDate = getEnvBool("LOGGER_FILE_APPENDDATE", defaultLogger.File.AppendDate)
|
defaultLogger.File.AppendDate = getEnvBool("LOGGER_FILE_APPENDDATE", defaultLogger.File.AppendDate)
|
||||||
|
|
Loading…
Reference in New Issue