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
d5c1c0e9
Commit
d5c1c0e9
authored
Apr 28, 2018
by
Waleed Gadelkareem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
log errors in access log and make static request logging optional
parent
8e61a6a6
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
62 additions
and
38 deletions
+62
-38
admin_test.go
admin_test.go
+1
-0
config.go
config.go
+2
-0
error.go
error.go
+4
-1
router.go
router.go
+55
-37
No files found.
admin_test.go
View file @
d5c1c0e9
...
@@ -67,6 +67,7 @@ func oldMap() map[string]interface{} {
...
@@ -67,6 +67,7 @@ func oldMap() map[string]interface{} {
m
[
"BConfig.WebConfig.Session.SessionDomain"
]
=
BConfig
.
WebConfig
.
Session
.
SessionDomain
m
[
"BConfig.WebConfig.Session.SessionDomain"
]
=
BConfig
.
WebConfig
.
Session
.
SessionDomain
m
[
"BConfig.WebConfig.Session.SessionDisableHTTPOnly"
]
=
BConfig
.
WebConfig
.
Session
.
SessionDisableHTTPOnly
m
[
"BConfig.WebConfig.Session.SessionDisableHTTPOnly"
]
=
BConfig
.
WebConfig
.
Session
.
SessionDisableHTTPOnly
m
[
"BConfig.Log.AccessLogs"
]
=
BConfig
.
Log
.
AccessLogs
m
[
"BConfig.Log.AccessLogs"
]
=
BConfig
.
Log
.
AccessLogs
m
[
"BConfig.Log.EnableStaticLogs"
]
=
BConfig
.
Log
.
EnableStaticLogs
m
[
"BConfig.Log.AccessLogsFormat"
]
=
BConfig
.
Log
.
AccessLogsFormat
m
[
"BConfig.Log.AccessLogsFormat"
]
=
BConfig
.
Log
.
AccessLogsFormat
m
[
"BConfig.Log.FileLineNum"
]
=
BConfig
.
Log
.
FileLineNum
m
[
"BConfig.Log.FileLineNum"
]
=
BConfig
.
Log
.
FileLineNum
m
[
"BConfig.Log.Outputs"
]
=
BConfig
.
Log
.
Outputs
m
[
"BConfig.Log.Outputs"
]
=
BConfig
.
Log
.
Outputs
...
...
config.go
View file @
d5c1c0e9
...
@@ -106,6 +106,7 @@ type SessionConfig struct {
...
@@ -106,6 +106,7 @@ type SessionConfig struct {
// LogConfig holds Log related config
// LogConfig holds Log related config
type
LogConfig
struct
{
type
LogConfig
struct
{
AccessLogs
bool
AccessLogs
bool
EnableStaticLogs
bool
//log static files requests default: false
AccessLogsFormat
string
//access log format: JSON_FORMAT, APACHE_FORMAT or empty string
AccessLogsFormat
string
//access log format: JSON_FORMAT, APACHE_FORMAT or empty string
FileLineNum
bool
FileLineNum
bool
Outputs
map
[
string
]
string
// Store Adaptor : config
Outputs
map
[
string
]
string
// Store Adaptor : config
...
@@ -247,6 +248,7 @@ func newBConfig() *Config {
...
@@ -247,6 +248,7 @@ func newBConfig() *Config {
},
},
Log
:
LogConfig
{
Log
:
LogConfig
{
AccessLogs
:
false
,
AccessLogs
:
false
,
EnableStaticLogs
:
false
,
AccessLogsFormat
:
"APACHE_FORMAT"
,
AccessLogsFormat
:
"APACHE_FORMAT"
,
FileLineNum
:
true
,
FileLineNum
:
true
,
Outputs
:
map
[
string
]
string
{
"console"
:
""
},
Outputs
:
map
[
string
]
string
{
"console"
:
""
},
...
...
error.go
View file @
d5c1c0e9
...
@@ -28,7 +28,7 @@ import (
...
@@ -28,7 +28,7 @@ import (
)
)
const
(
const
(
errorTypeHandler
=
iota
errorTypeHandler
=
iota
errorTypeController
errorTypeController
)
)
...
@@ -439,6 +439,9 @@ func exception(errCode string, ctx *context.Context) {
...
@@ -439,6 +439,9 @@ func exception(errCode string, ctx *context.Context) {
}
}
func
executeError
(
err
*
errorInfo
,
ctx
*
context
.
Context
,
code
int
)
{
func
executeError
(
err
*
errorInfo
,
ctx
*
context
.
Context
,
code
int
)
{
//make sure to log the error in the access log
logAccess
(
ctx
,
nil
,
code
)
if
err
.
errorType
==
errorTypeHandler
{
if
err
.
errorType
==
errorTypeHandler
{
ctx
.
ResponseWriter
.
WriteHeader
(
code
)
ctx
.
ResponseWriter
.
WriteHeader
(
code
)
err
.
handler
(
ctx
.
ResponseWriter
,
ctx
.
Request
)
err
.
handler
(
ctx
.
ResponseWriter
,
ctx
.
Request
)
...
...
router.go
View file @
d5c1c0e9
...
@@ -43,7 +43,7 @@ const (
...
@@ -43,7 +43,7 @@ const (
)
)
const
(
const
(
routerTypeBeego
=
iota
routerTypeBeego
=
iota
routerTypeRESTFul
routerTypeRESTFul
routerTypeHandler
routerTypeHandler
)
)
...
@@ -877,13 +877,15 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
...
@@ -877,13 +877,15 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
}
}
Admin
:
Admin
:
//admin module record QPS
//admin module record QPS
statusCode
:=
context
.
ResponseWriter
.
Status
statusCode
:=
context
.
ResponseWriter
.
Status
if
statusCode
==
0
{
if
statusCode
==
0
{
statusCode
=
200
statusCode
=
200
}
}
logAccess
(
context
,
&
startTime
,
statusCode
)
if
BConfig
.
Listen
.
EnableAdmin
{
if
BConfig
.
Listen
.
EnableAdmin
{
timeDur
:=
time
.
Since
(
startTime
)
timeDur
:=
time
.
Since
(
startTime
)
pattern
:=
""
pattern
:=
""
...
@@ -900,49 +902,30 @@ Admin:
...
@@ -900,49 +902,30 @@ Admin:
}
}
}
}
if
BConfig
.
RunMode
==
DEV
||
BConfig
.
Log
.
AccessLogs
{
if
BConfig
.
RunMode
==
DEV
&&
!
BConfig
.
Log
.
AccessLogs
{
timeDur
:=
time
.
Since
(
startTime
)
var
devInfo
string
var
devInfo
string
timeDur
:=
time
.
Since
(
startTime
)
iswin
:=
(
runtime
.
GOOS
==
"windows"
)
iswin
:=
(
runtime
.
GOOS
==
"windows"
)
statusColor
:=
logs
.
ColorByStatus
(
iswin
,
statusCode
)
statusColor
:=
logs
.
ColorByStatus
(
iswin
,
statusCode
)
methodColor
:=
logs
.
ColorByMethod
(
iswin
,
r
.
Method
)
methodColor
:=
logs
.
ColorByMethod
(
iswin
,
r
.
Method
)
resetColor
:=
logs
.
ColorByMethod
(
iswin
,
""
)
resetColor
:=
logs
.
ColorByMethod
(
iswin
,
""
)
if
BConfig
.
Log
.
AccessLogsFormat
!=
""
{
if
findRouter
{
record
:=
&
logs
.
AccessLogRecord
{
if
routerInfo
!=
nil
{
RemoteAddr
:
context
.
Input
.
IP
(),
devInfo
=
fmt
.
Sprintf
(
"|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s r:%s"
,
context
.
Input
.
IP
(),
statusColor
,
statusCode
,
RequestTime
:
startTime
,
resetColor
,
timeDur
.
String
(),
"match"
,
methodColor
,
r
.
Method
,
resetColor
,
r
.
URL
.
Path
,
RequestMethod
:
r
.
Method
,
routerInfo
.
pattern
)
Request
:
fmt
.
Sprintf
(
"%s %s %s"
,
r
.
Method
,
r
.
RequestURI
,
r
.
Proto
),
ServerProtocol
:
r
.
Proto
,
Host
:
r
.
Host
,
Status
:
statusCode
,
ElapsedTime
:
timeDur
,
HTTPReferrer
:
r
.
Header
.
Get
(
"Referer"
),
HTTPUserAgent
:
r
.
Header
.
Get
(
"User-Agent"
),
RemoteUser
:
r
.
Header
.
Get
(
"Remote-User"
),
BodyBytesSent
:
0
,
//@todo this one is missing!
}
logs
.
AccessLog
(
record
,
BConfig
.
Log
.
AccessLogsFormat
)
}
else
{
if
findRouter
{
if
routerInfo
!=
nil
{
devInfo
=
fmt
.
Sprintf
(
"|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s r:%s"
,
context
.
Input
.
IP
(),
statusColor
,
statusCode
,
resetColor
,
timeDur
.
String
(),
"match"
,
methodColor
,
r
.
Method
,
resetColor
,
r
.
URL
.
Path
,
routerInfo
.
pattern
)
}
else
{
devInfo
=
fmt
.
Sprintf
(
"|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s"
,
context
.
Input
.
IP
(),
statusColor
,
statusCode
,
resetColor
,
timeDur
.
String
(),
"match"
,
methodColor
,
r
.
Method
,
resetColor
,
r
.
URL
.
Path
)
}
}
else
{
}
else
{
devInfo
=
fmt
.
Sprintf
(
"|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s"
,
context
.
Input
.
IP
(),
statusColor
,
statusCode
,
resetColor
,
devInfo
=
fmt
.
Sprintf
(
"|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s"
,
context
.
Input
.
IP
(),
statusColor
,
statusCode
,
resetColor
,
timeDur
.
String
(),
"nomatch"
,
methodColor
,
r
.
Method
,
resetColor
,
r
.
URL
.
Path
)
timeDur
.
String
(),
"match"
,
methodColor
,
r
.
Method
,
resetColor
,
r
.
URL
.
Path
)
}
if
iswin
{
logs
.
W32Debug
(
devInfo
)
}
else
{
logs
.
Debug
(
devInfo
)
}
}
}
else
{
devInfo
=
fmt
.
Sprintf
(
"|%15s|%s %3d %s|%13s|%8s|%s %-7s %s %-3s"
,
context
.
Input
.
IP
(),
statusColor
,
statusCode
,
resetColor
,
timeDur
.
String
(),
"nomatch"
,
methodColor
,
r
.
Method
,
resetColor
,
r
.
URL
.
Path
)
}
if
iswin
{
logs
.
W32Debug
(
devInfo
)
}
else
{
logs
.
Debug
(
devInfo
)
}
}
}
}
// Call WriteHeader if status code has been set changed
// Call WriteHeader if status code has been set changed
...
@@ -991,3 +974,38 @@ func toURL(params map[string]string) string {
...
@@ -991,3 +974,38 @@ func toURL(params map[string]string) string {
}
}
return
strings
.
TrimRight
(
u
,
"&"
)
return
strings
.
TrimRight
(
u
,
"&"
)
}
}
func
logAccess
(
ctx
*
beecontext
.
Context
,
startTime
*
time
.
Time
,
statusCode
int
)
{
//Skip logging if AccessLogs config is false
if
!
BConfig
.
Log
.
AccessLogs
{
return
}
//Skip logging static requests unless EnableStaticLogs config is true
if
!
BConfig
.
Log
.
EnableStaticLogs
&&
DefaultAccessLogFilter
.
Filter
(
ctx
)
{
return
}
var
(
requestTime
time
.
Time
elapsedTime
time
.
Duration
r
=
ctx
.
Request
)
if
startTime
!=
nil
{
requestTime
=
*
startTime
elapsedTime
=
time
.
Since
(
*
startTime
)
}
record
:=
&
logs
.
AccessLogRecord
{
RemoteAddr
:
ctx
.
Input
.
IP
(),
RequestTime
:
requestTime
,
RequestMethod
:
r
.
Method
,
Request
:
fmt
.
Sprintf
(
"%s %s %s"
,
r
.
Method
,
r
.
RequestURI
,
r
.
Proto
),
ServerProtocol
:
r
.
Proto
,
Host
:
r
.
Host
,
Status
:
statusCode
,
ElapsedTime
:
elapsedTime
,
HTTPReferrer
:
r
.
Header
.
Get
(
"Referer"
),
HTTPUserAgent
:
r
.
Header
.
Get
(
"User-Agent"
),
RemoteUser
:
r
.
Header
.
Get
(
"Remote-User"
),
BodyBytesSent
:
0
,
//@todo this one is missing!
}
logs
.
AccessLog
(
record
,
BConfig
.
Log
.
AccessLogsFormat
)
}
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