Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
3f9e723c
authored
Sep 17, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: create new log file when too many logs recorded
parent
04e474f0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
3 deletions
+27
-3
common/logger.go
+26
-2
main.go
+1
-1
No files found.
common/logger.go
View file @
3f9e723c
...
...
@@ -8,6 +8,7 @@ import (
"log"
"os"
"path/filepath"
"sync"
"time"
)
...
...
@@ -17,9 +18,24 @@ const (
loggerError
=
"ERR"
)
func
SetupGinLog
()
{
const
maxLogCount
=
1000000
var
logCount
int
var
setupLogLock
sync
.
Mutex
var
setupLogWorking
bool
func
SetupLogger
()
{
if
*
LogDir
!=
""
{
logPath
:=
filepath
.
Join
(
*
LogDir
,
fmt
.
Sprintf
(
"oneapi-%s.log"
,
time
.
Now
()
.
Format
(
"20060102150405"
)))
ok
:=
setupLogLock
.
TryLock
()
if
!
ok
{
log
.
Println
(
"setup log is already working"
)
return
}
defer
func
()
{
setupLogLock
.
Unlock
()
setupLogWorking
=
false
}()
logPath
:=
filepath
.
Join
(
*
LogDir
,
fmt
.
Sprintf
(
"oneapi-%s.log"
,
time
.
Now
()
.
Format
(
"20060102"
)))
fd
,
err
:=
os
.
OpenFile
(
logPath
,
os
.
O_APPEND
|
os
.
O_CREATE
|
os
.
O_WRONLY
,
0644
)
if
err
!=
nil
{
log
.
Fatal
(
"failed to open log file"
)
...
...
@@ -59,6 +75,14 @@ func logHelper(ctx context.Context, level string, msg string) {
id
:=
ctx
.
Value
(
RequestIdKey
)
now
:=
time
.
Now
()
_
,
_
=
fmt
.
Fprintf
(
writer
,
"[%s] %v | %s | %s
\n
"
,
level
,
now
.
Format
(
"2006/01/02 - 15:04:05"
),
id
,
msg
)
logCount
++
// we don't need accurate count, so no lock here
if
logCount
>
maxLogCount
&&
!
setupLogWorking
{
logCount
=
0
setupLogWorking
=
true
go
func
()
{
SetupLogger
()
}()
}
}
func
FatalLog
(
v
...
any
)
{
...
...
main.go
View file @
3f9e723c
...
...
@@ -21,7 +21,7 @@ var buildFS embed.FS
var
indexPage
[]
byte
func
main
()
{
common
.
Setup
GinLog
()
common
.
Setup
Logger
()
common
.
SysLog
(
"One API "
+
common
.
Version
+
" started"
)
if
os
.
Getenv
(
"GIN_MODE"
)
!=
"debug"
{
gin
.
SetMode
(
gin
.
ReleaseMode
)
...
...
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