blob: 226a9094f2ae88556c81bd263b7c673aecc9403a [file] [log] [blame]
Mohamed Abukar2e78e422019-06-02 11:45:52 +03001/*
2==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17==================================================================================
18*/
19
20package xapp
21
Mohamed Abukar2e78e422019-06-02 11:45:52 +030022import (
Mohamed Abukarb4c70392020-02-13 20:43:15 +020023 "fmt"
Mohamed Abukar775722c2019-06-10 16:41:57 +030024 mdclog "gerrit.o-ran-sc.org/r/com/golog"
Mohamed Abukar3e611c62019-07-05 13:40:11 +030025 "time"
Mohamed Abukar2e78e422019-06-02 11:45:52 +030026)
27
28type Log struct {
Mohamed Abukar775722c2019-06-10 16:41:57 +030029 logger *mdclog.MdcLogger
Mohamed Abukar2e78e422019-06-02 11:45:52 +030030}
31
Mohamed Abukar775722c2019-06-10 16:41:57 +030032func NewLogger(name string) *Log {
33 l, _ := mdclog.InitLogger(name)
34 return &Log{
35 logger: l,
36 }
Mohamed Abukar2e78e422019-06-02 11:45:52 +030037}
38
Mohamed Abukar522d7362020-11-25 09:41:01 +020039func (l *Log) SetFormat(logMonitor int) {
40 l.logger.Mdclog_format_initialize(logMonitor)
41}
42
Mohamed Abukar775722c2019-06-10 16:41:57 +030043func (l *Log) SetLevel(level int) {
44 l.logger.LevelSet(mdclog.Level(level))
Mohamed Abukar2e78e422019-06-02 11:45:52 +030045}
46
Mohamed Abukar775722c2019-06-10 16:41:57 +030047func (l *Log) SetMdc(key string, value string) {
48 l.logger.MdcAdd(key, value)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030049}
50
Mohamed Abukarc1f00b72020-11-25 13:51:00 +020051func (l *Log) GetLevel() mdclog.Level {
52 return l.logger.LevelGet()
53}
54
Mohamed Abukar775722c2019-06-10 16:41:57 +030055func (l *Log) Error(pattern string, args ...interface{}) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +030056 if l.logger.LevelGet() < mdclog.ERR {
57 return
58 }
Mohamed Abukarb4c70392020-02-13 20:43:15 +020059 l.SetMdc("time", timeFormat())
Mohamed Abukar775722c2019-06-10 16:41:57 +030060 l.logger.Error(pattern, args...)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030061}
62
Mohamed Abukar775722c2019-06-10 16:41:57 +030063func (l *Log) Warn(pattern string, args ...interface{}) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +030064 if l.logger.LevelGet() < mdclog.WARN {
65 return
66 }
Mohamed Abukarb4c70392020-02-13 20:43:15 +020067 l.SetMdc("time", timeFormat())
Mohamed Abukar775722c2019-06-10 16:41:57 +030068 l.logger.Warning(pattern, args...)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030069}
70
Mohamed Abukar775722c2019-06-10 16:41:57 +030071func (l *Log) Info(pattern string, args ...interface{}) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +030072 if l.logger.LevelGet() < mdclog.INFO {
73 return
74 }
Mohamed Abukarb4c70392020-02-13 20:43:15 +020075 l.SetMdc("time", timeFormat())
Mohamed Abukar775722c2019-06-10 16:41:57 +030076 l.logger.Info(pattern, args...)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030077}
78
Mohamed Abukar775722c2019-06-10 16:41:57 +030079func (l *Log) Debug(pattern string, args ...interface{}) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +030080 if l.logger.LevelGet() < mdclog.DEBUG {
81 return
82 }
Mohamed Abukarb4c70392020-02-13 20:43:15 +020083 l.SetMdc("time", timeFormat())
Mohamed Abukar775722c2019-06-10 16:41:57 +030084 l.logger.Debug(pattern, args...)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030085}
Mohamed Abukarb4c70392020-02-13 20:43:15 +020086
87func timeFormat() string {
88 t := time.Now()
89 return fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
90}