Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
f8c4b3aa
Commit
f8c4b3aa
authored
Feb 03, 2016
by
JessonChan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add files logger to separate different logs
parent
51b1095e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
73 additions
and
15 deletions
+73
-15
files.go
logs/files.go
+58
-13
log.go
logs/log.go
+0
-2
logger.go
logs/logger.go
+15
-0
No files found.
logs/files.go
View file @
f8c4b3aa
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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
logs
import
"time"
import
(
"encoding/json"
"time"
)
// A filesLogWriter manages several fileLogWriter
// filesLogWriter will write logs to the file in json configuration and write the same level log to correspond file
// means if the file name in configuration is project.log filesLogWriter will create project.error.log/project.debug.log
// and write the error-level logs to project.error.log and write the debug-level logs to project.debug.log
// the rotate attribute also acts like fileLogWriter
type
filesLogWriter
struct
{
writers
[
LevelDebug
+
1
]
*
fileLogWriter
writers
[
LevelDebug
+
1
+
1
]
*
fileLogWriter
// the last one for fullLogWriter
fullLogWriter
*
fileLogWriter
Separate
[]
string
`json:"separate"`
}
var
levelNames
=
[
...
]
string
{
"emergency"
,
"alert"
,
"critical"
,
"error"
,
"warning"
,
"notice"
,
"info"
,
"debug"
}
// Init file logger with json config.
// jsonConfig like:
// {
// "filename":"logs/beego.log",
// "maxLines":0,
// "maxsize":0,
// "daily":true,
// "maxDays":15,
// "rotate":true,
// "perm":0600,
// "separate":["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"],
// }
func
(
f
*
filesLogWriter
)
Init
(
config
string
)
error
{
writer
:=
newFileWriter
()
.
(
*
fileLogWriter
)
err
:=
writer
.
Init
(
config
)
if
err
!=
nil
{
return
err
}
f
.
writers
[
0
]
=
writer
f
.
fullLogWriter
=
writer
f
.
writers
[
LevelDebug
+
1
]
=
writer
json
.
Unmarshal
([]
byte
(
config
),
f
)
for
i
:=
LevelEmergency
;
i
<=
f
.
writers
[
0
]
.
Level
;
i
++
{
writer
=
newFileWriter
()
.
(
*
fileLogWriter
)
writer
.
Init
(
config
)
writer
.
Level
=
i
writer
.
fileNameOnly
+=
"."
+
levelNames
[
i
]
f
.
writers
[
i
+
1
]
=
writer
for
i
:=
LevelEmergency
;
i
<
LevelDebug
+
1
;
i
++
{
for
_
,
v
:=
range
f
.
Separate
{
if
v
==
levelNames
[
i
]
{
writer
=
newFileWriter
()
.
(
*
fileLogWriter
)
writer
.
Init
(
config
)
writer
.
Level
=
i
writer
.
fileNameOnly
+=
"."
+
levelNames
[
i
]
f
.
writers
[
i
]
=
writer
}
}
}
return
nil
...
...
@@ -34,10 +80,10 @@ func (f *filesLogWriter) Destroy() {
}
func
(
f
*
filesLogWriter
)
WriteMsg
(
when
time
.
Time
,
msg
string
,
level
int
)
error
{
if
f
.
writers
[
0
]
!=
nil
{
f
.
writers
[
0
]
.
WriteMsg
(
when
,
msg
,
level
)
if
f
.
fullLogWriter
!=
nil
{
f
.
fullLogWriter
.
WriteMsg
(
when
,
msg
,
level
)
}
for
i
:=
1
;
i
<
len
(
f
.
writers
)
;
i
++
{
for
i
:=
0
;
i
<
len
(
f
.
writers
)
-
1
;
i
++
{
if
f
.
writers
[
i
]
!=
nil
{
if
level
==
f
.
writers
[
i
]
.
Level
{
f
.
writers
[
i
]
.
WriteMsg
(
when
,
msg
,
level
)
...
...
@@ -55,7 +101,6 @@ func (f *filesLogWriter) Flush() {
}
}
// newFilesWriter create a FileLogWriter returning as LoggerInterface.
func
newFilesWriter
()
Logger
{
return
&
filesLogWriter
{}
...
...
logs/log.go
View file @
f8c4b3aa
...
...
@@ -64,8 +64,6 @@ const (
LevelWarn
=
LevelWarning
)
var
levelNames
=
[
...
]
string
{
"emergency"
,
"alert"
,
"critical"
,
"error"
,
"warning"
,
"notice"
,
"info"
,
"debug"
}
type
loggerType
func
()
Logger
// Logger defines the behavior of a log provider.
...
...
logs/logger.go
View file @
f8c4b3aa
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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
logs
import
(
...
...
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