Adding healthcheck endpoint for sms
[aaf/sms.git] / sms-service / src / sms / log / logger.go
1 /*
2  * Copyright 2018 Intel Corporation, Inc
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package log
18
19 import (
20         "fmt"
21         "log"
22         "os"
23 )
24
25 var errL, warnL, infoL *log.Logger
26 var stdErr, stdWarn, stdInfo *log.Logger
27
28 // Init will be called by sms.go before any other packages use it
29 func Init(filePath string) {
30
31         stdErr = log.New(os.Stderr, "ERROR: ", log.Lshortfile|log.LstdFlags)
32         stdWarn = log.New(os.Stdout, "WARNING: ", log.Lshortfile|log.LstdFlags)
33         stdInfo = log.New(os.Stdout, "INFO: ", log.Lshortfile|log.LstdFlags)
34
35         if filePath == "" {
36                 // We will just to std streams
37                 return
38         }
39
40         f, err := os.Create(filePath)
41         if err != nil {
42                 stdErr.Println("Unable to create log file: " + err.Error())
43                 return
44         }
45
46         errL = log.New(f, "ERROR: ", log.Lshortfile|log.LstdFlags)
47         warnL = log.New(f, "WARNING: ", log.Lshortfile|log.LstdFlags)
48         infoL = log.New(f, "INFO: ", log.Lshortfile|log.LstdFlags)
49 }
50
51 // WriteError writes output to the writer we have
52 // defined during its creation with ERROR prefix
53 func WriteError(msg string) {
54         if errL != nil {
55                 errL.Output(2, fmt.Sprintln(msg))
56         }
57         if stdErr != nil {
58                 stdErr.Output(2, fmt.Sprintln(msg))
59         }
60 }
61
62 // WriteWarn writes output to the writer we have
63 // defined during its creation with WARNING prefix
64 func WriteWarn(msg string) {
65         if warnL != nil {
66                 warnL.Output(2, fmt.Sprintln(msg))
67         }
68         if stdWarn != nil {
69                 stdWarn.Output(2, fmt.Sprintln(msg))
70         }
71 }
72
73 // WriteInfo writes output to the writer we have
74 // defined during its creation with INFO prefix
75 func WriteInfo(msg string) {
76         if infoL != nil {
77                 infoL.Output(2, fmt.Sprintln(msg))
78         }
79         if stdInfo != nil {
80                 stdInfo.Output(2, fmt.Sprintln(msg))
81         }
82 }
83
84 //CheckError is a helper function to reduce
85 //repetition of error checking blocks of code
86 func CheckError(err error, topic string) error {
87         if err != nil {
88                 msg := topic + ": " + err.Error()
89                 if errL != nil {
90                         errL.Output(2, fmt.Sprintln(msg))
91                 }
92                 if stdErr != nil {
93                         stdErr.Output(2, fmt.Sprintln(msg))
94                 }
95                 return err
96         }
97         return nil
98 }