blob: 69bed09bdfaec652e054827d5fdc4a98a6ff6dcf [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 Abukar775722c2019-06-10 16:41:57 +030051func (l *Log) Error(pattern string, args ...interface{}) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +030052 if l.logger.LevelGet() < mdclog.ERR {
53 return
54 }
Mohamed Abukarb4c70392020-02-13 20:43:15 +020055 l.SetMdc("time", timeFormat())
Mohamed Abukar775722c2019-06-10 16:41:57 +030056 l.logger.Error(pattern, args...)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030057}
58
Mohamed Abukar775722c2019-06-10 16:41:57 +030059func (l *Log) Warn(pattern string, args ...interface{}) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +030060 if l.logger.LevelGet() < mdclog.WARN {
61 return
62 }
Mohamed Abukarb4c70392020-02-13 20:43:15 +020063 l.SetMdc("time", timeFormat())
Mohamed Abukar775722c2019-06-10 16:41:57 +030064 l.logger.Warning(pattern, args...)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030065}
66
Mohamed Abukar775722c2019-06-10 16:41:57 +030067func (l *Log) Info(pattern string, args ...interface{}) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +030068 if l.logger.LevelGet() < mdclog.INFO {
69 return
70 }
Mohamed Abukarb4c70392020-02-13 20:43:15 +020071 l.SetMdc("time", timeFormat())
Mohamed Abukar775722c2019-06-10 16:41:57 +030072 l.logger.Info(pattern, args...)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030073}
74
Mohamed Abukar775722c2019-06-10 16:41:57 +030075func (l *Log) Debug(pattern string, args ...interface{}) {
Juha Hyttinend28b8dd2020-05-27 09:21:08 +030076 if l.logger.LevelGet() < mdclog.DEBUG {
77 return
78 }
Mohamed Abukarb4c70392020-02-13 20:43:15 +020079 l.SetMdc("time", timeFormat())
Mohamed Abukar775722c2019-06-10 16:41:57 +030080 l.logger.Debug(pattern, args...)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030081}
Mohamed Abukarb4c70392020-02-13 20:43:15 +020082
83func timeFormat() string {
84 t := time.Now()
85 return fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
86}