Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | package xapp |
| 21 | |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 22 | import ( |
Mohamed Abukar | b4c7039 | 2020-02-13 20:43:15 +0200 | [diff] [blame] | 23 | "fmt" |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 24 | mdclog "gerrit.o-ran-sc.org/r/com/golog" |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 25 | "time" |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | type Log struct { |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 29 | logger *mdclog.MdcLogger |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 30 | } |
| 31 | |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 32 | func NewLogger(name string) *Log { |
| 33 | l, _ := mdclog.InitLogger(name) |
| 34 | return &Log{ |
| 35 | logger: l, |
| 36 | } |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 37 | } |
| 38 | |
Mohamed Abukar | 522d736 | 2020-11-25 09:41:01 +0200 | [diff] [blame] | 39 | func (l *Log) SetFormat(logMonitor int) { |
| 40 | l.logger.Mdclog_format_initialize(logMonitor) |
| 41 | } |
| 42 | |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 43 | func (l *Log) SetLevel(level int) { |
| 44 | l.logger.LevelSet(mdclog.Level(level)) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 45 | } |
| 46 | |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 47 | func (l *Log) SetMdc(key string, value string) { |
| 48 | l.logger.MdcAdd(key, value) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 49 | } |
| 50 | |
Mohamed Abukar | c1f00b7 | 2020-11-25 13:51:00 +0200 | [diff] [blame] | 51 | func (l *Log) GetLevel() mdclog.Level { |
| 52 | return l.logger.LevelGet() |
| 53 | } |
| 54 | |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 55 | func (l *Log) Error(pattern string, args ...interface{}) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 56 | if l.logger.LevelGet() < mdclog.ERR { |
| 57 | return |
| 58 | } |
Mohamed Abukar | b4c7039 | 2020-02-13 20:43:15 +0200 | [diff] [blame] | 59 | l.SetMdc("time", timeFormat()) |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 60 | l.logger.Error(pattern, args...) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 61 | } |
| 62 | |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 63 | func (l *Log) Warn(pattern string, args ...interface{}) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 64 | if l.logger.LevelGet() < mdclog.WARN { |
| 65 | return |
| 66 | } |
Mohamed Abukar | b4c7039 | 2020-02-13 20:43:15 +0200 | [diff] [blame] | 67 | l.SetMdc("time", timeFormat()) |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 68 | l.logger.Warning(pattern, args...) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 69 | } |
| 70 | |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 71 | func (l *Log) Info(pattern string, args ...interface{}) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 72 | if l.logger.LevelGet() < mdclog.INFO { |
| 73 | return |
| 74 | } |
Mohamed Abukar | b4c7039 | 2020-02-13 20:43:15 +0200 | [diff] [blame] | 75 | l.SetMdc("time", timeFormat()) |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 76 | l.logger.Info(pattern, args...) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 77 | } |
| 78 | |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 79 | func (l *Log) Debug(pattern string, args ...interface{}) { |
Juha Hyttinen | d28b8dd | 2020-05-27 09:21:08 +0300 | [diff] [blame] | 80 | if l.logger.LevelGet() < mdclog.DEBUG { |
| 81 | return |
| 82 | } |
Mohamed Abukar | b4c7039 | 2020-02-13 20:43:15 +0200 | [diff] [blame] | 83 | l.SetMdc("time", timeFormat()) |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 84 | l.logger.Debug(pattern, args...) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 85 | } |
Mohamed Abukar | b4c7039 | 2020-02-13 20:43:15 +0200 | [diff] [blame] | 86 | |
| 87 | func 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 | } |