Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor and update util to use new log formatter setting #21

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 46 additions & 37 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,85 +1,94 @@
package main

import (
"fmt"
"os"
"path/filepath"
"runtime/debug"

"github.com/asaskevich/govalidator"
"github.com/urfave/cli"

"github.com/free5gc/udm/internal/logger"
"github.com/free5gc/udm/internal/util"
"github.com/free5gc/udm/pkg/factory"
"github.com/free5gc/udm/pkg/service"
logger_util "github.com/free5gc/util/logger"
"github.com/free5gc/util/version"
)

var UDM = &service.UDM{}
var UDM *service.UdmApp

func main() {
defer func() {
if p := recover(); p != nil {
// Print stack for panic to log. Fatalf() will let program exit.
logger.AppLog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
logger.MainLog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
}
}()

app := cli.NewApp()
app.Name = "udm"
app.Usage = "5G Unified Data Management (UDM)"
app.Action = action
app.Flags = UDM.GetCliCmd()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "Load configuration from `FILE`",
},
cli.StringSliceFlag{
Name: "log, l",
Usage: "Output NF log to `FILE`",
},
}

if err := app.Run(os.Args); err != nil {
fmt.Printf("UDM Run error: %v\n", err)
logger.MainLog.Errorf("UDM Run error: %v\n", err)
}
}

func action(c *cli.Context) error {
if err := initLogFile(c.String("log"), c.String("log5gc")); err != nil {
logger.AppLog.Errorf("%+v", err)
func action(cliCtx *cli.Context) error {
tlsKeyLogPath, err := initLogFile(cliCtx.StringSlice("log"))
if err != nil {
return err
}

if err := UDM.Initialize(c); err != nil {
switch errType := err.(type) {
case govalidator.Errors:
validErrs := err.(govalidator.Errors).Errors()
for _, validErr := range validErrs {
logger.CfgLog.Errorf("%+v", validErr)
}
default:
logger.CfgLog.Errorf("%+v", errType)
}
logger.CfgLog.Errorf("[-- PLEASE REFER TO SAMPLE CONFIG FILE COMMENTS --]")
return fmt.Errorf("Failed to initialize !!")
}
logger.MainLog.Infoln("UDM version: ", version.GetVersion())

logger.AppLog.Infoln(c.App.Name)
logger.AppLog.Infoln("UDM version: ", version.GetVersion())
cfg, err := factory.ReadConfig(cliCtx.String("config"))
if err != nil {
return err
}
factory.UdmConfig = cfg
udm, err := service.NewApp(cfg)
if err != nil {
return err
}
UDM = udm

UDM.Start()
udm.Start(tlsKeyLogPath)

return nil
}

func initLogFile(logNfPath, log5gcPath string) error {
UDM.KeyLogPath = util.UdmDefaultKeyLogPath
func initLogFile(logNfPath []string) (string, error) {
logTlsKeyPath := ""

if err := logger.LogFileHook(logNfPath, log5gcPath); err != nil {
return err
}
for _, path := range logNfPath {
if err := logger_util.LogFileHook(logger.Log, path); err != nil {
return "", err
}

if logNfPath != "" {
nfDir, _ := filepath.Split(logNfPath)
if logTlsKeyPath != "" {
continue
}

nfDir, _ := filepath.Split(path)
tmpDir := filepath.Join(nfDir, "key")
if err := os.MkdirAll(tmpDir, 0o775); err != nil {
logger.InitLog.Errorf("Make directory %s failed: %+v", tmpDir, err)
return err
return "", err
}
_, name := filepath.Split(util.UdmDefaultKeyLogPath)
UDM.KeyLogPath = filepath.Join(tmpDir, name)
_, name := filepath.Split(factory.UdmDefaultTLSKeyLogPath)
logTlsKeyPath = filepath.Join(tmpDir, name)
}

return nil
return logTlsKeyPath, nil
}
39 changes: 35 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
module github.com/free5gc/udm

go 1.14
go 1.17

require (
github.com/antihax/optional v1.0.0
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/free5gc/openapi v1.0.4
github.com/free5gc/util v1.0.3
github.com/free5gc/openapi v1.0.6
github.com/free5gc/util v1.0.5-0.20230306071612-a52909216bd2
github.com/gin-gonic/gin v1.7.7
github.com/google/uuid v1.3.0
github.com/sirupsen/logrus v1.8.1
github.com/urfave/cli v1.22.5
golang.org/x/crypto v0.1.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang-jwt/jwt v3.2.1+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/tim-ywliu/nested-logrus-formatter v1.3.2 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/h2non/gock.v1 v1.1.2 // indirect
)
Loading