Logging module with common file logger 81/12081/1
authorSANDEEP KUMAR JAISAWAL <s.jaisawal@samsung.com>
Wed, 22 Nov 2023 16:12:28 +0000 (21:42 +0530)
committerSANDEEP KUMAR JAISAWAL <s.jaisawal@samsung.com>
Wed, 22 Nov 2023 16:15:34 +0000 (21:45 +0530)
Issue-Id: AIMLFW-64

Change-Id: I2c676ccc520a70d5c207152c8d2acc532fe1b191
Signed-off-by: SANDEEP KUMAR JAISAWAL <s.jaisawal@samsung.com>
apis/mmes_apis.go
config.env
core/s3_manager.go
logging/logging.go [new file with mode: 0644]
main.go

index 479c23c..2acaec7 100644 (file)
@@ -19,12 +19,12 @@ package apis
 
 import (
        "encoding/json"
-       "fmt"
        "io"
        "net/http"
        "os"
 
        "example.com/mmes/core"
+       "example.com/mmes/logging"
        "github.com/gin-gonic/gin"
 )
 
@@ -38,7 +38,7 @@ type MMESApis struct {
 }
 
 func init() {
-       fmt.Println("Starting api server...")
+       logging.INFO("Starting api server...")
        router := gin.Default()
 
        router.POST("/registerModel", RegisterModel)
@@ -48,14 +48,14 @@ func init() {
        router.POST("/uploadModel/:modelName", UploadModel)
        router.GET("/downloadModel/:modelName", DownloadModel)
        router.Run(os.Getenv("MMES_URL"))
-       fmt.Println("Started api server...")
+       logging.INFO("Started api server...")
 }
 
 func RegisterModel(cont *gin.Context) {
        var returnCode int = http.StatusCreated
        var responseMsg string = "Model registered successfully"
 
-       fmt.Println("Creating model...")
+       logging.INFO("Creating model...")
        bodyBytes, _ := io.ReadAll(cont.Request.Body)
 
        var modelInfo ModelInfo
@@ -63,14 +63,13 @@ func RegisterModel(cont *gin.Context) {
        //data such as model name, rapp id etc
        err := json.Unmarshal(bodyBytes, &modelInfo)
        if err != nil || modelInfo.ModelName == "" {
-               fmt.Println("Error in unmarshalling")
+               logging.ERROR("Error in unmarshalling")
                cont.JSON(http.StatusBadRequest, gin.H{
                        "code":    http.StatusBadRequest,
                        "message": string("Can not parse input data, provide mandatory details"),
                })
-               //return
        } else {
-               fmt.Println(modelInfo.ModelName, modelInfo.RAppId, modelInfo.Metainfo)
+               logging.INFO(modelInfo.ModelName, modelInfo.RAppId, modelInfo.Metainfo)
                modelInfoBytes, _ := json.Marshal(modelInfo)
 
                //TODO Create singleton for s3_manager
@@ -96,13 +95,13 @@ input :
        Model name : string
 */
 func GetModelInfo(cont *gin.Context) {
-       fmt.Println("Fetching model")
+       logging.INFO("Fetching model")
        bodyBytes, _ := io.ReadAll(cont.Request.Body)
        //TODO Error checking of request is not in json, i.e. etra ',' at EOF
        jsonMap := make(map[string]interface{})
        json.Unmarshal(bodyBytes, &jsonMap)
        model_name := jsonMap["model-name"].(string)
-       fmt.Println("The request model name: ", model_name)
+       logging.INFO("The request model name: ", model_name)
 
        s3_manager := core.NewS3Manager()
        model_info := s3_manager.GetBucketObject(model_name+os.Getenv("INFO_FILE_PREFIX"), model_name)
@@ -117,7 +116,7 @@ func GetModelInfo(cont *gin.Context) {
 Provides the model details by param model name
 */
 func GetModelInfoByName(cont *gin.Context) {
-       fmt.Println("Get model info by name API ...")
+       logging.INFO("Get model info by name API ...")
        modelName := cont.Param("modelName")
 
        s3_manager := core.NewS3Manager()
@@ -133,7 +132,7 @@ func GetModelInfoByName(cont *gin.Context) {
 // TODO : Model version as input
 
 func UploadModel(cont *gin.Context) {
-       fmt.Println("Uploading model API ...")
+       logging.INFO("Uploading model API ...")
        modelName := cont.Param("modelName")
        //TODO convert multipart.FileHeader to []byted
        fileHeader, _ := cont.FormFile("file")
@@ -143,11 +142,9 @@ func UploadModel(cont *gin.Context) {
        defer file.Close()
        byteFile, _ := io.ReadAll((file))
 
-       fmt.Println("Uploading model : ", modelName)
-       fmt.Println("Recieved file name :", fileHeader.Filename)
-
+       logging.INFO("Uploading model : ", modelName)
        s3_manager := core.NewS3Manager()
-       s3_manager.UploadFile(byteFile, modelName+os.Getenv("MODEL_NAME_PREFIX"), modelName)
+       s3_manager.UploadFile(byteFile, modelName+os.Getenv("MODEL_FILE_PREFIX"), modelName)
        cont.JSON(http.StatusOK, gin.H{
                "code":    http.StatusOK,
                "message": string("Model uploaded successfully.."),
@@ -159,9 +156,9 @@ API to download the trained model from s3 bucket
 Input: model name in path params as "modelName"
 */
 func DownloadModel(cont *gin.Context) {
-       fmt.Println("Download model API ...")
+       logging.INFO("Download model API ...")
        modelName := cont.Param("modelName")
-       fileName := modelName + os.Getenv("MODEL_NAME_PREFIX")
+       fileName := modelName + os.Getenv("MODEL_FILE_PREFIX")
        s3_manager := core.NewS3Manager()
        fileByes := s3_manager.GetBucketObject(fileName, modelName)
 
@@ -172,16 +169,16 @@ func DownloadModel(cont *gin.Context) {
 }
 
 func GetModel(cont *gin.Context) {
-       fmt.Println("Fetching model")
+       logging.INFO("Fetching model")
        cont.IndentedJSON(http.StatusOK, " ")
 }
 
 func UpdateModel() {
-       fmt.Println("Updating model...")
+       logging.INFO("Updating model...")
        return
 }
 
 func DeleteModel() {
-       fmt.Println("Deleting model...")
+       logging.INFO("Deleting model...")
        return
 }
index 054a6cb..a825749 100644 (file)
@@ -1,7 +1,8 @@
-MMES_URL=
-S3_URL=
+MMES_URL=localhost:8082
+S3_URL=http://localhost:32080
 S3_ACCESS_KEY=
 S3_SECRET_KEY=
 S3_REGION=
-MODEL_FILE_PREFIX="_model.zip"
-INFO_FILE_PREFIX="_info.json"
+MODEL_FILE_PREFIX=_model.zip
+INFO_FILE_PREFIX=_info.json
+LOG_FILE_NAME=mmes.log
index 6059165..a8e39f3 100644 (file)
@@ -20,10 +20,10 @@ package core
 import (
        "bytes"
        "errors"
-       "fmt"
        "io"
        "os"
 
+       "example.com/mmes/logging"
        "github.com/aws/aws-sdk-go/aws"
        "github.com/aws/aws-sdk-go/aws/awserr"
        "github.com/aws/aws-sdk-go/aws/credentials"
@@ -76,7 +76,7 @@ func (s3manager *S3Manager) CreateBucket(bucketName string) (err error) {
        _, s3Err := s3manager.S3Client.CreateBucket(&s3.CreateBucketInput{Bucket: aws.String(bucketName)})
 
        if s3Err != nil {
-               fmt.Println(s3Err)
+               logging.ERROR(s3Err)
                //Convert the aws to get the code/error msg for api response
                if aerr, ok := s3Err.(awserr.Error); ok {
                        err = errors.New(aerr.Message())
@@ -101,11 +101,10 @@ func (s3manager *S3Manager) GetBucketObject(objectName string, bucketName string
        defer result.Body.Close()
 
        if err != nil {
-               fmt.Println("Error, can't get fetch object..")
+               logging.ERROR("Error, can't get fetch object..")
                return response
-       } else {
-               fmt.Println("Successfully retrieved object...")
        }
+       logging.INFO("Successfully retrieved object...")
        //TODO : Error handling
        response, err = io.ReadAll(result.Body)
        return response
@@ -118,9 +117,9 @@ func (s3manager *S3Manager) DeleteBucket(client *s3.S3, objectName string, bucke
                        Bucket: aws.String(bucketName),
                }
                client.DeleteBucket(deleteBucketInput)
-               fmt.Println("Bucket deleted successfully..")
+               logging.INFO("Bucket deleted successfully..")
        } else {
-               fmt.Println("Failed to delete the Bucket ...")
+               logging.ERROR("Failed to delete the Bucket ...")
        }
 
 }
@@ -132,10 +131,10 @@ func (s3manager *S3Manager) DeleteBucketObject(client *s3.S3, objectName string,
        }
        _, err := client.DeleteObject(deleteInput)
        if err != nil {
-               fmt.Println("Can not delete the bucket object")
+               logging.WARN("Can not delete the bucket object")
                return false
        }
-       fmt.Println("Object deleted successfully..")
+       logging.INFO("Object deleted successfully..")
        return true
 }
 
@@ -149,18 +148,18 @@ func (s3manager *S3Manager) UploadFile(dataBytes []byte, file_name string, bucke
        }
        _, err := s3manager.S3Client.PutObject(params)
        if err != nil {
-               fmt.Println("Error in uploading file to bucket ", err)
+               logging.ERROR("Error in uploading file to bucket ", err)
        }
-       fmt.Println("File uploaded to bucket ", bucketName)
+       logging.INFO("File uploaded to bucket ", bucketName)
 }
 
 func (s3manager *S3Manager) ListBucket(client *s3.S3) {
        input := &s3.ListBucketsInput{}
        result, err := client.ListBuckets(input)
        if err != nil {
-               fmt.Println(err.Error())
+               logging.ERROR(err.Error())
        }
-       fmt.Println(result)
+       logging.INFO(result)
 }
 
 // Return list of objects in the buckets
diff --git a/logging/logging.go b/logging/logging.go
new file mode 100644 (file)
index 0000000..f4cbcea
--- /dev/null
@@ -0,0 +1,38 @@
+package logging
+
+import (
+       "log"
+       "os"
+)
+
+var infoLogger *log.Logger
+var warnLogger *log.Logger
+var errorLogger *log.Logger
+
+func init() {
+       logFile, fileErr := os.Create(os.Getenv("LOG_FILE_NAME"))
+       if fileErr != nil {
+               log.Fatal("Can not start MMES service,issue in creating log file")
+       }
+       flags := log.Ldate | log.Ltime 
+       infoLogger = log.New(logFile, "INFO:", flags)
+       warnLogger = log.New(logFile, "WARN:", flags)
+       errorLogger = log.New(logFile, "ERROR:", flags)
+
+       INFO("Loggers loaded ..")
+}
+
+// Prefixes INFO for each log message
+func INFO(logParams ...interface{}) {
+       infoLogger.Println(logParams...)
+}
+
+// Prefixes WARN for each log message
+func WARN(logParams ...interface{}) {
+       warnLogger.Println(logParams...)
+}
+
+// Prefixes ERROR for each log message
+func ERROR(logParams ...interface{}) {
+       errorLogger.Println(logParams...)
+}
diff --git a/main.go b/main.go
index 9192876..b67444b 100644 (file)
--- a/main.go
+++ b/main.go
@@ -18,14 +18,13 @@ limitations under the License.
 package main
 
 import (
-       "fmt"
-
        "example.com/mmes/apis"
+       "example.com/mmes/logging"
 )
 
 func main() {
-       fmt.Println("Starting api..")
+       logging.INFO("Starting api..")
        mmes := &apis.MMESApis{}
 
-       fmt.Println(mmes)
+       _ = mmes
 }