diff options
author | 2018-02-28 17:06:29 -0800 | |
---|---|---|
committer | 2018-03-01 08:51:26 -0800 | |
commit | 7f0c6c6ff3a180827c539d696d33df44abd7491e (patch) | |
tree | 9764bb4e9bd06f8d19faf4998e30c53b1c4d487c /sms-service | |
parent | 5b194b5776a242bb7a13fec6c0be8ee5ecdfcd06 (diff) |
Adding logging framework
Adding a framework for logging
We will add other logging integrations into this framework
Issue-ID: AAF-148
Change-Id: Ia16d1ddf4c24a5e2f957c429aff23970d081ccc4
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
Diffstat (limited to 'sms-service')
-rw-r--r-- | sms-service/src/sms/backend/backend.go | 2 | ||||
-rw-r--r-- | sms-service/src/sms/config/config.go | 1 | ||||
-rw-r--r-- | sms-service/src/sms/log/logger.go | 65 |
3 files changed, 68 insertions, 0 deletions
diff --git a/sms-service/src/sms/backend/backend.go b/sms-service/src/sms/backend/backend.go index 2cc3798..a1055e6 100644 --- a/sms-service/src/sms/backend/backend.go +++ b/sms-service/src/sms/backend/backend.go @@ -18,6 +18,7 @@ package backend import ( smsconfig "sms/config" + smslogger "sms/log" ) // SecretDomain is where Secrets are stored. @@ -66,6 +67,7 @@ func InitSecretBackend() (SecretBackend, error) { err := backendImpl.Init() if err != nil { + smslogger.WriteError(err.Error()) return nil, err } diff --git a/sms-service/src/sms/config/config.go b/sms-service/src/sms/config/config.go index 15f3660..23af251 100644 --- a/sms-service/src/sms/config/config.go +++ b/sms-service/src/sms/config/config.go @@ -43,6 +43,7 @@ func ReadConfigFile(file string) (*SMSConfiguration, error) { if err != nil { return nil, err } + defer f.Close() SMSConfig = &SMSConfiguration{} decoder := json.NewDecoder(f) diff --git a/sms-service/src/sms/log/logger.go b/sms-service/src/sms/log/logger.go new file mode 100644 index 0000000..ad7d388 --- /dev/null +++ b/sms-service/src/sms/log/logger.go @@ -0,0 +1,65 @@ +/* + * Copyright 2018 Intel Corporation, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package log + +import ( + "log" + "os" +) + +var errLogger *log.Logger +var warnLogger *log.Logger +var infoLogger *log.Logger + +func init() { + f, err := os.OpenFile("sms.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + if err != nil { + log.Println("Unable to create a log file") + log.Println(err) + errLogger = log.New(os.Stderr, "ERROR: ", log.Lshortfile) + warnLogger = log.New(os.Stdout, "WARNING: ", log.Lshortfile) + infoLogger = log.New(os.Stdout, "INFO: ", log.Lshortfile) + } else { + errLogger = log.New(f, "ERROR: ", log.Lshortfile) + warnLogger = log.New(f, "WARNING: ", log.Lshortfile) + infoLogger = log.New(f, "INFO: ", log.Lshortfile) + } +} + +// WriteError writes output to the writer we have +// defined durint its creation with ERROR prefix +func WriteError(msg string) { + if errLogger != nil { + errLogger.Println(msg) + } +} + +// WriteWarn writes output to the writer we have +// defined durint its creation with WARNING prefix +func WriteWarn(msg string) { + if warnLogger != nil { + warnLogger.Println(msg) + } +} + +// WriteInfo writes output to the writer we have +// defined durint its creation with INFO prefix +func WriteInfo(msg string) { + if infoLogger != nil { + infoLogger.Println(msg) + } +} |