-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The zerolog functionality is not worth the effort to maintain the dependency tree brought by this package from the packaging point of view. We can always revert this is it turns out that we need performance capabilities of the library.
- Loading branch information
Showing
10 changed files
with
441 additions
and
76 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package logging | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type LogLevel int | ||
|
||
const ( | ||
DEBUG LogLevel = iota | ||
INFO | ||
WARN | ||
ERROR | ||
) | ||
|
||
func (self LogLevel) String() string { | ||
return [...]string{"DEBUG", "INFO", "WARN", "ERROR"}[self] | ||
} | ||
|
||
type Logger struct { | ||
Level LogLevel | ||
Timestamp bool | ||
metadata map[string]interface{} | ||
logfile *os.File | ||
} | ||
|
||
func NewLogger(level LogLevel, path string) (*Logger, error) { | ||
var logger Logger | ||
logger.Level = level | ||
logger.Timestamp = false | ||
logger.metadata = make(map[string]interface{}) | ||
|
||
logfile, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) | ||
if err != nil { | ||
return nil, err | ||
} | ||
logger.logfile = logfile | ||
|
||
return &logger, nil | ||
} | ||
|
||
func (self *Logger) Destroy() error { | ||
return self.logfile.Close() | ||
} | ||
|
||
func (self *Logger) Metadata(metadata map[string]interface{}) { | ||
self.metadata = metadata | ||
} | ||
|
||
func (self *Logger) formatMetadata() (string, error) { | ||
var build strings.Builder | ||
if len(self.metadata) > 0 { | ||
joiner := "" | ||
for key, item := range self.metadata { | ||
_, err := fmt.Fprintf(&build, "%s%s: %v", joiner, key, item) | ||
if err != nil { | ||
return build.String(), err | ||
} | ||
if len(joiner) == 0 { | ||
joiner = ", " | ||
} | ||
} | ||
} | ||
// clear metadata for next use | ||
self.metadata = make(map[string]interface{}) | ||
return build.String(), nil | ||
} | ||
|
||
func (self *Logger) writeRecord(level LogLevel, message string) error { | ||
metadata, err := self.formatMetadata() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var build strings.Builder | ||
if self.Timestamp { | ||
_, err = build.WriteString(time.Now().Format("2006-01-02 15:04:05 ")) | ||
} | ||
|
||
_, err = build.WriteString(fmt.Sprintf("[%s] ", level)) | ||
if err != nil { | ||
return nil | ||
} | ||
_, err = build.WriteString(message) | ||
if err != nil { | ||
return nil | ||
} | ||
if len(metadata) > 0 { | ||
_, err = build.WriteString(fmt.Sprintf(" [%s]", metadata)) | ||
if err != nil { | ||
return nil | ||
} | ||
} | ||
_, err = build.WriteString("\n") | ||
if err != nil { | ||
return nil | ||
} | ||
_, err = self.logfile.WriteString(build.String()) | ||
return err | ||
} | ||
|
||
func (self *Logger) Debug(message string) error { | ||
if self.Level == DEBUG { | ||
return self.writeRecord(DEBUG, message) | ||
} | ||
return nil | ||
} | ||
|
||
func (self *Logger) Info(message string) error { | ||
if self.Level <= INFO { | ||
return self.writeRecord(INFO, message) | ||
} | ||
return nil | ||
} | ||
|
||
func (self *Logger) Warn(message string) error { | ||
if self.Level <= WARN { | ||
return self.writeRecord(WARN, message) | ||
} | ||
return nil | ||
} | ||
|
||
func (self *Logger) Error(message string) error { | ||
if self.Level <= ERROR { | ||
return self.writeRecord(ERROR, message) | ||
} | ||
return nil | ||
} |
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.