Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
utils
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
golang kittool
utils
Commits
0603f3c6
Commit
0603f3c6
authored
Mar 30, 2022
by
feidy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update logger
parent
a74e79a9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
26 deletions
+37
-26
logger.go
logger.go
+37
-26
No files found.
logger.go
View file @
0603f3c6
package
utils
import
(
"os"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
)
const
(
// DebugLevel logs are typically voluminous, and are usually disabled in
// production.
...
...
@@ -29,20 +29,20 @@ const (
Fatal
)
type
loggerConfig
struct
{
Level
int8
FilePath
string
MaxSize
int
Level
int8
FilePath
string
MaxSize
int
MaxBackups
int
MaxAge
int
Compress
bool
MaxAge
int
Compress
bool
Ostd
bool
}
type
loggerOption
func
(
m
*
loggerConfig
)
func
WithLevel
(
level
int8
)
loggerOption
{
return
func
(
m
*
loggerConfig
){
return
func
(
m
*
loggerConfig
)
{
m
.
Level
=
level
}
}
...
...
@@ -53,39 +53,43 @@ func WithLogFilePath(filePath string) loggerOption {
}
}
func
WithMaxSize
(
maxSize
int
)
loggerOption
{
func
WithMaxSize
(
maxSize
int
)
loggerOption
{
return
func
(
m
*
loggerConfig
)
{
m
.
MaxSize
=
maxSize
}
}
func
WithMaxBackups
(
maxBack
int
)
loggerOption
{
func
WithMaxBackups
(
maxBack
int
)
loggerOption
{
return
func
(
m
*
loggerConfig
)
{
m
.
MaxBackups
=
maxBack
}
}
func
WithMaxAge
(
maxAge
int
)
loggerOption
{
func
WithMaxAge
(
maxAge
int
)
loggerOption
{
return
func
(
m
*
loggerConfig
)
{
m
.
MaxAge
=
maxAge
}
}
func
WithCompress
(
compress
bool
)
loggerOption
{
return
func
(
m
*
loggerConfig
){
return
func
(
m
*
loggerConfig
)
{
m
.
Compress
=
compress
}
}
func
WithOstd
(
isOstd
bool
)
loggerOption
{
return
func
(
m
*
loggerConfig
)
{
m
.
Ostd
=
isOstd
}
}
var
lf
=
loggerConfig
{
Level
:
1
,
FilePath
:
"log/log.log"
,
MaxSize
:
100
,
MaxAge
:
7
,
Level
:
1
,
FilePath
:
"log/log.log"
,
MaxSize
:
100
,
MaxAge
:
7
,
MaxBackups
:
7
,
Compress
:
true
,
Compress
:
true
,
}
var
L
*
zap
.
SugaredLogger
...
...
@@ -98,11 +102,19 @@ func InitLogger(opt ...loggerOption) {
writeSync
:=
getLogWriter
()
encoder
:=
getEncoder
()
consoleDebugging
:=
zapcore
.
Lock
(
os
.
Stdout
)
core
:=
zapcore
.
NewTee
(
zapcore
.
NewCore
(
encoder
,
writeSync
,
zapcore
.
Level
(
lf
.
Level
)),
zapcore
.
NewCore
(
zapcore
.
NewConsoleEncoder
(
zap
.
NewDevelopmentEncoderConfig
()),
consoleDebugging
,
zapcore
.
Level
(
lf
.
Level
)),
)
var
core
zapcore
.
Core
if
lf
.
Ostd
{
consoleDebugging
:=
zapcore
.
Lock
(
os
.
Stdout
)
core
=
zapcore
.
NewTee
(
zapcore
.
NewCore
(
encoder
,
writeSync
,
zapcore
.
Level
(
lf
.
Level
)),
zapcore
.
NewCore
(
zapcore
.
NewConsoleEncoder
(
zap
.
NewDevelopmentEncoderConfig
()),
consoleDebugging
,
zapcore
.
Level
(
lf
.
Level
)),
)
}
else
{
core
=
zapcore
.
NewTee
(
zapcore
.
NewCore
(
encoder
,
writeSync
,
zapcore
.
Level
(
lf
.
Level
)),
)
}
logger
:=
zap
.
New
(
core
,
zap
.
AddCaller
())
L
=
logger
.
Sugar
()
}
...
...
@@ -125,4 +137,3 @@ func getLogWriter() zapcore.WriteSyncer {
return
zapcore
.
AddSync
(
lumberJackLogger
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment