From: hoejoo.lee Date: Thu, 13 Apr 2023 09:45:31 +0000 (-0400) Subject: added logger X-Git-Tag: 1.0.0~40 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=6cf690832be62bfff9eb88cdb86df0bd8ab8b43f;p=aiml-fw%2Faihp%2Fips%2Fkserve-adapter.git added logger Change-Id: I0318634935e813b3196e6b668effab176ac93c0f Signed-off-by: hoejoo.lee --- diff --git a/go.mod b/go.mod new file mode 100755 index 0000000..59f1cb7 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter + +go 1.18 diff --git a/main.go b/main.go new file mode 100755 index 0000000..c8e2956 --- /dev/null +++ b/main.go @@ -0,0 +1,26 @@ +/* +================================================================================== + Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved. + + 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 main + +import "gerrit.o-ran-sc.org/r/aiml-fw/aihp/ips/kserve-adapter" + +func main() { + logger.Logging(logger.DEBUG, "IN") + defer logger.Logging(logger.DEBUG, "OUT") +} diff --git a/pkg/commons/logger/logger.go b/pkg/commons/logger/logger.go new file mode 100755 index 0000000..6fa6800 --- /dev/null +++ b/pkg/commons/logger/logger.go @@ -0,0 +1,62 @@ +/* +================================================================================== +Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved. +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 commons/logger implements log stream. +package logger + +import ( + "log" + "os" + "path" + "runtime" + "strconv" + "strings" +) + +var loggers [3]*log.Logger +var logFlag int + +// init initializes package global value. +func init() { + logFlag = log.Ldate | log.Ltime + loggers[0] = log.New(os.Stdout, "[INFO][kserve-adapter]", logFlag) + loggers[1] = log.New(os.Stdout, "[DEBUG][kserve-adapter]", logFlag) + loggers[2] = log.New(os.Stdout, "[ERROR][kserve-adapter]", logFlag) +} + +const ( + INFO = iota + DEBUG + ERROR +) + +// Logging prints log stream on standard output with file name and function name, line. +func Logging(level int, msgs ...string) { + pc, file, line, _ := runtime.Caller(1) + _, fileName := path.Split(file) + parts := strings.Split(runtime.FuncForPC(pc).Name(), ".") + pl := len(parts) + funcName := parts[pl-1] + + packageName := "" + if parts[pl-2][0] == '(' { + funcName = parts[pl-2] + "." + funcName + packageName = strings.Join(parts[0:pl-2], ".") + } else { + packageName = strings.Join(parts[0:pl-1], ".") + } + + loggers[level].Println(packageName, fileName, funcName, ":", strconv.Itoa(line), msgs) +}