Skip to content

Commit

Permalink
Merge pull request #55 from cloud-barista/feature/update-logger
Browse files Browse the repository at this point in the history
Feature/update logger
  • Loading branch information
heedaeshin authored Sep 12, 2024
2 parents 086eafb + 578c0ce commit 564e985
Show file tree
Hide file tree
Showing 47 changed files with 2,026 additions and 1,201 deletions.
6 changes: 2 additions & 4 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ package cmd

import (
"github.com/cloud-barista/mc-data-manager/internal/execfunc"
"github.com/cloud-barista/mc-data-manager/internal/log"
"github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -36,9 +35,8 @@ Semi-structured data: json, xml
You must enter the data size in GB.`,
Run: func(_ *cobra.Command, _ []string) {
logrus.SetFormatter(&log.CustomTextFormatter{CmdName: "create", JobName: "dummy create"})
if err := execfunc.DummyCreate(commandTask); err != nil {
logrus.Errorf("dummy create failed : %v", err)
log.Error().Str("CmdName", "create").Str("JobName", "dummy create").Msgf("dummy create failed : %v", err)
}
},
}
Expand Down
11 changes: 5 additions & 6 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ package cmd
import (
"os"

"github.com/cloud-barista/mc-data-manager/internal/log"
"github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -35,14 +34,14 @@ var deleteLocalCmd = &cobra.Command{
Use: "local",

Run: func(cmd *cobra.Command, args []string) {
logrus.SetFormatter(&log.CustomTextFormatter{CmdName: "delete"})
logrus.WithFields(logrus.Fields{"jobName": "local delete"}).Info("start deleting local data")
// logrus.SetFormatter(&log.CustomTextFormatter{CmdName: "delete"})
log.Info().Msg("start deleting local data")

if err := os.RemoveAll(commandTask.Directory); err != nil {
logrus.WithFields(logrus.Fields{"jobName": "local delete"}).Errorf("failed to delete local : %v", err)
log.Error().Str("jobName", "local delete").Msgf("failed to delete local : %v", err)
return
}
logrus.Infof("successfully deleted : %s\n", commandTask.Directory)
log.Info().Msgf("successfully deleted : %s\n", commandTask.Directory)

},
}
Expand Down
4 changes: 0 additions & 4 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ limitations under the License.
package cmd

import (
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/spf13/cobra"
)
Expand All @@ -32,7 +29,6 @@ var serverCmd = &cobra.Command{
Short: "Start Web Server",
Long: `Start Web Server`,
Run: func(cmd *cobra.Command, args []string) {
log.GetInstance().NewLogEntry().WithCmdName("server").WithJobName("web Server").WithLevel(zerolog.InfoLevel).WithMessage("Start Web Server")
dmsv.Run(dmsv.InitServer(listenPort, allowIP...), listenPort)
},
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ package cmd

import (
"github.com/cloud-barista/mc-data-manager/internal/execfunc"
"github.com/cloud-barista/mc-data-manager/internal/log"
"github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -28,9 +27,8 @@ var testCmd = &cobra.Command{
Short: "test-command",
Long: `test-command`,
Run: func(_ *cobra.Command, _ []string) {
logrus.SetFormatter(&log.CustomTextFormatter{CmdName: "test", JobName: "test dummy create"})
if err := execfunc.DummyCreate(commandTask); err != nil {
logrus.Errorf("test dummy create failed : %v", err)
log.Error().Msgf("test dummy create failed : %v", err)
}
},
}
Expand Down
58 changes: 58 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,66 @@ import (
"os"
"path/filepath"
"sync"

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

var Settings InitConfig

type InitConfig struct {
Profile ProfileConfig `mapstructure:"profile"`
Logger LogConfig `mapstructure:"log"`
}

type ProfileConfig struct {
Default string `mapstructure:"default"`
}

type LogConfig struct {
ZeroConfig `mapstructure:",squash"`
File LumberConfig `mapstructure:",squash"`
}

type ZeroConfig struct {
LogLevel string `mapstructure:"level"`
LogWriter string `mapstructure:"writer"`
}
type LumberConfig struct {
Path string `mapstructure:"filepath"`
MaxSize int `mapstructure:"maxsize"`
MaxBackups int `mapstructure:"maxbackups"`
MaxAge int `mapstructure:"maxage"`
Compress bool `mapstructure:"compress"`
}

// init
func Init() {
execPath, err := os.Executable()
if err != nil {
log.Error().Err(err).Msg("Failed to get executable path")
log.Info().Msg("Using Default Config")
}
viper.AddConfigPath(filepath.Join(execPath, "../../data/var/run/data-manager/config"))
viper.AddConfigPath(filepath.Join(execPath, "/data/var/run/data-manager/config"))
viper.AddConfigPath(filepath.Join(execPath, "./"))
viper.AddConfigPath(filepath.Join(execPath, "./config/"))
viper.SetConfigName("config")

err = viper.ReadInConfig()
if err != nil {
log.Error().Err(err).Msg("Failed to read config file")
return
}

err = viper.Unmarshal(&Settings)
if err != nil {
log.Fatal().Err(err).Msg("Unable to decode into struct")
}
log.Debug().Msgf("config params : %+v", Settings)
log.Info().Str("loglevel", Settings.Logger.LogLevel).Msg("Logger initialized with loglevel")
}

// ConfigManager structure definition
type ConfigManager struct {
DefaultProfile string
Expand Down
13 changes: 12 additions & 1 deletion data/var/run/data-manager/config/config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
{
"defaultProfile":"default"
"profile":{
"default":"default"
},
"log":{
"level":"debug",
"writer":"both",
"filepath": "./data/var/log/data-manager.log",
"maxsize": 1000,
"maxbackups": 3,
"maxage": 30,
"compress": false
}
}
Loading

0 comments on commit 564e985

Please sign in to comment.