Skip to content

Commit

Permalink
Merge pull request #52 from cloud-barista/feature/update-docker-zerolog
Browse files Browse the repository at this point in the history
Update : docker compose , Add : Zero log
  • Loading branch information
heedaeshin authored Sep 10, 2024
2 parents 5aee7be + 50e8ae6 commit 5bb2ae4
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 10 deletions.
8 changes: 4 additions & 4 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ limitations under the License.
package cmd

import (
"github.com/cloud-barista/mc-data-manager/internal/log"
log "github.com/cloud-barista/mc-data-manager/internal/zerolog"
"github.com/rs/zerolog"

dmsv "github.com/cloud-barista/mc-data-manager/websrc/serve"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -31,8 +32,7 @@ var serverCmd = &cobra.Command{
Short: "Start Web Server",
Long: `Start Web Server`,
Run: func(cmd *cobra.Command, args []string) {
logrus.SetFormatter(&log.CustomTextFormatter{CmdName: "server", JobName: "web server"})
logrus.Info("Start Web Server")
log.GetInstance().NewLogEntry().WithCmdName("server").WithJobName("web Server").WithLevel(zerolog.InfoLevel).WithMessage("Start Web Server")
dmsv.Run(dmsv.InitServer(listenPort, allowIP...), listenPort)
},
}
Expand Down
10 changes: 5 additions & 5 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ services:
- "3300:3300"
restart : always
volumes:
- ./log:/app/log/
- ./data:/app/data/
- /etc/localtime:/etc/localtime:ro
env_file:
- .env
Expand All @@ -47,8 +47,8 @@ services:
# deploy:
# resources:
# limits:
# cpus: "0.50" # Capacity CPU : 0.5 (50% of a single CPU core)
# memory: "512M" # Capacity Mem : 512MB
# cpus: "2.0" # Capacity CPU : 2.0 (100% of a Dual CPU core)
# memory: "1024M" # Capacity Mem : 1024MB
# reservations:
# cpus: "0.25" # allocated CPU: 0.25 (25% of a single CPU core)
# memory: "256M" # allocated Mem: 256MB
# cpus: "0.5" # allocated CPU: 0.25 (25% of a single CPU core)
# memory: "512M" # allocated Mem: 256MB
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ require (
github.com/labstack/gommon v0.4.2 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/rs/zerolog v1.33.0 // indirect
github.com/swaggo/files/v2 v2.0.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -100,6 +101,7 @@ github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+Gr
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -162,15 +164,20 @@ github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJ
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
Expand Down Expand Up @@ -267,6 +274,7 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
171 changes: 171 additions & 0 deletions internal/zerolog/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
Copyright 2023 The Cloud-Barista Authors.
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 (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

var (
instance *Logger
once sync.Once
)

type Logger struct {
zerolog.Logger
}

type LogEntry struct {
logger *Logger
level zerolog.Level
cmdName string
jobName string
message string
}

// GetInstance returns the singleton instance of Logger
func GetInstance() *Logger {
once.Do(func() {
instance = &Logger{
Logger: log.Output(zerolog.ConsoleWriter{Out: os.Stderr}),
}
instance.setupLogger()
})
return instance
}

func (l *Logger) setupLogger() {
execPath, err := os.Executable()
if err != nil {
log.Fatal().Msgf("Failed to get executable path: %v", err)
}

// Get the directory path of the binary file
execDir := filepath.Dir(execPath)

// Set the log directory path
logDir := filepath.Join(execDir, "./data/var/log")

// Create the log directory
if err := os.MkdirAll(logDir, os.ModePerm); err != nil {
log.Fatal().Msgf("Failed to create log directory: %v", err)
}

// Set the log file path
logFilePath := filepath.Join(logDir, "data-manager.log")

// Open or create the log file
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_RDWR, os.FileMode(0644))
if err != nil {
log.Fatal().Msgf("Failed to create log file: %v", err)
}

// Set zerolog level and output
l.Logger = l.Output(io.MultiWriter(os.Stdout, logFile)).With().Timestamp().Logger()
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}

func (l *Logger) NewLogEntry() *LogEntry {
return &LogEntry{
logger: l,
}
}

func (e *LogEntry) WithLevel(level zerolog.Level) *LogEntry {
e.level = level
return e
}

func (e *LogEntry) WithCmdName(cmdName string) *LogEntry {
e.cmdName = cmdName
return e
}

func (e *LogEntry) WithJobName(jobName string) *LogEntry {
e.jobName = jobName
return e
}

func (e *LogEntry) WithMessage(message string) *LogEntry {
e.message = message
return e
}

func (e *LogEntry) logWithCustomFormat() {
timeFormatted := time.Now().Format("2006-01-02T15:04:05-07:00")
logEvent := e.logger.With().
Str("time", timeFormatted).
Str("level", e.level.String()).
Str("cmdName", e.cmdName).
Str("jobName", e.jobName).
Logger()

logEvent.Log().Msg(strings.ToUpper(e.message[:1]) + e.message[1:])
}

func Debug(cmdName, jobName string, args ...interface{}) {
GetInstance().NewLogEntry().
WithLevel(zerolog.DebugLevel).
WithCmdName(cmdName).
WithJobName(jobName).
WithMessage(fmt.Sprint(args...)).
logWithCustomFormat()
}

func Info(cmdName, jobName string, args ...interface{}) {
GetInstance().NewLogEntry().
WithLevel(zerolog.InfoLevel).
WithCmdName(cmdName).
WithJobName(jobName).
WithMessage(fmt.Sprint(args...)).
logWithCustomFormat()
}

func Warn(cmdName, jobName string, args ...interface{}) {
GetInstance().NewLogEntry().
WithLevel(zerolog.WarnLevel).
WithCmdName(cmdName).
WithJobName(jobName).
WithMessage(fmt.Sprint(args...)).
logWithCustomFormat()
}

func Error(cmdName, jobName string, args ...interface{}) {
GetInstance().NewLogEntry().
WithLevel(zerolog.ErrorLevel).
WithCmdName(cmdName).
WithJobName(jobName).
WithMessage(fmt.Sprint(args...)).
logWithCustomFormat()
}

func Fatal(cmdName, jobName string, args ...interface{}) {
GetInstance().NewLogEntry().
WithLevel(zerolog.FatalLevel).
WithCmdName(cmdName).
WithJobName(jobName).
WithMessage(fmt.Sprint(args...)).
logWithCustomFormat()
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ limitations under the License.
*/
package main

import "github.com/cloud-barista/mc-data-manager/cmd"
import (
"github.com/cloud-barista/mc-data-manager/cmd"
log "github.com/cloud-barista/mc-data-manager/internal/zerolog"
)

func main() {
log.Info("Data Manager", "Startup", "Is starting")
cmd.Execute()
log.Info("Data Manager", "Shutdown", "Is shutting down")

}

0 comments on commit 5bb2ae4

Please sign in to comment.