package xapp
-/*
-#cgo CFLAGS: -I/usr/local/include
-#cgo LDFLAGS: -lmdclog
-#
-#include <mdclog/mdclog.h>
-void xAppMgr_mdclog_write(mdclog_severity_t severity, const char *msg) {
- mdclog_write(severity, "%s", msg);
-}
-*/
-import "C"
-
import (
"fmt"
- "log"
+ mdclog "gerrit.o-ran-sc.org/r/com/golog"
"time"
)
type Log struct {
+ logger *mdclog.MdcLogger
}
-const (
- LogLvlErr = C.MDCLOG_ERR
- LogLvlWarn = C.MDCLOG_WARN
- LogLvlInfo = C.MDCLOG_INFO
- LogLvlDebug = C.MDCLOG_DEBUG
-)
+func NewLogger(name string) *Log {
+ l, _ := mdclog.InitLogger(name)
+ return &Log{
+ logger: l,
+ }
+}
-func WriteLog(lvl C.mdclog_severity_t, msg string) {
- t := time.Now().Format("2019-01-02 15:04:05")
- text := fmt.Sprintf("%s:: %s ", t, msg)
+func (l *Log) SetFormat(logMonitor int) {
+ l.logger.Mdclog_format_initialize(logMonitor)
+}
- C.xAppMgr_mdclog_write(lvl, C.CString(text))
+func (l *Log) SetLevel(level int) {
+ l.logger.LevelSet(mdclog.Level(level))
}
-func (Log) SetLevel(level int) {
- l := C.mdclog_severity_t(level)
- C.mdclog_level_set(l)
+func (l *Log) SetMdc(key string, value string) {
+ l.logger.MdcAdd(key, value)
}
-func (Log) SetMdc(key string, value string) {
- C.mdclog_mdc_add(C.CString(key), C.CString(value))
+func (l *Log) GetLevel() mdclog.Level {
+ return l.logger.LevelGet()
}
-func (Log) Fatal(pattern string, args ...interface{}) {
- WriteLog(LogLvlErr, fmt.Sprintf(pattern, args...))
- log.Panic("Fatal error occured, exiting ...")
+func (l *Log) Error(pattern string, args ...interface{}) {
+ if l.logger.LevelGet() < mdclog.ERR {
+ return
+ }
+ l.SetMdc("time", timeFormat())
+ l.logger.Error(pattern, args...)
}
-func (Log) Error(pattern string, args ...interface{}) {
- WriteLog(LogLvlErr, fmt.Sprintf(pattern, args...))
+func (l *Log) Warn(pattern string, args ...interface{}) {
+ if l.logger.LevelGet() < mdclog.WARN {
+ return
+ }
+ l.SetMdc("time", timeFormat())
+ l.logger.Warning(pattern, args...)
}
-func (Log) Warn(pattern string, args ...interface{}) {
- WriteLog(LogLvlWarn, fmt.Sprintf(pattern, args...))
+func (l *Log) Info(pattern string, args ...interface{}) {
+ if l.logger.LevelGet() < mdclog.INFO {
+ return
+ }
+ l.SetMdc("time", timeFormat())
+ l.logger.Info(pattern, args...)
}
-func (Log) Info(pattern string, args ...interface{}) {
- WriteLog(LogLvlInfo, fmt.Sprintf(pattern, args...))
+func (l *Log) Debug(pattern string, args ...interface{}) {
+ if l.logger.LevelGet() < mdclog.DEBUG {
+ return
+ }
+ l.SetMdc("time", timeFormat())
+ l.logger.Debug(pattern, args...)
}
-func (Log) Debug(pattern string, args ...interface{}) {
- WriteLog(LogLvlDebug, fmt.Sprintf(pattern, args...))
+func timeFormat() string {
+ t := time.Now()
+ return fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
}