Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
7578298b
Commit
7578298b
authored
Mar 28, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rewrote access log middleware
parent
d2892fc7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
43 deletions
+94
-43
middleware/log/log.go
middleware/log/log.go
+94
-43
No files found.
middleware/log/log.go
View file @
7578298b
// Package log implements basic but useful request logging middleware.
// Package log implements basic but useful request
(access)
logging middleware.
package
log
package
log
import
(
import
(
...
@@ -11,61 +11,112 @@ import (
...
@@ -11,61 +11,112 @@ import (
// New instantiates a new instance of logging middleware.
// New instantiates a new instance of logging middleware.
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
func
New
(
c
middleware
.
Controller
)
(
middleware
.
Middleware
,
error
)
{
var
logWhat
,
outputFile
,
format
string
rules
,
err
:=
parse
(
c
)
var
logger
*
log
.
Logger
if
err
!=
nil
{
return
nil
,
err
}
for
c
.
Next
()
{
// Open the log files for writing when the server starts
c
.
Args
(
&
logWhat
,
&
outputFile
,
&
format
)
c
.
Startup
(
func
()
error
{
for
i
:=
0
;
i
<
len
(
rules
);
i
++
{
var
err
error
var
file
*
os
.
File
if
logWhat
==
""
{
if
rules
[
i
]
.
OutputFile
==
"stdout"
{
return
nil
,
c
.
ArgErr
()
file
=
os
.
Stdout
}
}
else
if
rules
[
i
]
.
OutputFile
==
"stderr"
{
if
outputFile
==
""
{
file
=
os
.
Stderr
outputFile
=
defaultLogFilename
}
else
{
file
,
err
=
os
.
OpenFile
(
rules
[
i
]
.
OutputFile
,
os
.
O_RDWR
|
os
.
O_CREATE
|
os
.
O_APPEND
,
0644
)
if
err
!=
nil
{
return
err
}
}
rules
[
i
]
.
Log
=
log
.
New
(
file
,
""
,
0
)
}
}
switch
format
{
case
""
:
return
nil
format
=
defaultReqLogFormat
})
case
"{common}"
:
format
=
commonLogFormat
return
func
(
next
middleware
.
HandlerFunc
)
middleware
.
HandlerFunc
{
case
"{combined}"
:
return
Logger
{
Next
:
next
,
Rules
:
rules
}
.
ServeHTTP
format
=
combinedLogFormat
},
nil
}
func
(
l
Logger
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
for
_
,
rule
:=
range
l
.
Rules
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
PathScope
)
{
responseRecorder
:=
middleware
.
NewResponseRecorder
(
w
)
status
,
err
:=
l
.
Next
(
responseRecorder
,
r
)
rep
:=
middleware
.
NewReplacer
(
r
,
responseRecorder
)
rule
.
Log
.
Println
(
rep
.
Replace
(
rule
.
Format
))
return
status
,
err
}
}
}
}
return
l
.
Next
(
w
,
r
)
}
// Open the log file for writing when the server starts
func
parse
(
c
middleware
.
Controller
)
([]
LogRule
,
error
)
{
c
.
Startup
(
func
()
error
{
var
rules
[]
LogRule
var
err
error
var
file
*
os
.
File
for
c
.
Next
()
{
args
:=
c
.
RemainingArgs
()
if
outputFile
==
"stdout"
{
if
len
(
args
)
==
0
{
file
=
os
.
Stdout
// Nothing specified; use defaults
}
else
if
outputFile
==
"stderr"
{
rules
=
append
(
rules
,
LogRule
{
file
=
os
.
Stderr
PathScope
:
"/"
,
OutputFile
:
defaultLogFilename
,
Format
:
defaultLogFormat
,
})
}
else
if
len
(
args
)
==
1
{
// Only an output file specified
rules
=
append
(
rules
,
LogRule
{
PathScope
:
"/"
,
OutputFile
:
args
[
0
],
Format
:
defaultLogFormat
,
})
}
else
{
}
else
{
file
,
err
=
os
.
OpenFile
(
outputFile
,
os
.
O_RDWR
|
os
.
O_CREATE
|
os
.
O_APPEND
,
0644
)
// Path scope, output file, and maybe a format specified
if
err
!=
nil
{
return
err
format
:=
defaultLogFormat
if
len
(
args
)
>
2
{
switch
args
[
2
]
{
case
"{common}"
:
format
=
commonLogFormat
case
"{combined}"
:
format
=
combinedLogFormat
}
}
}
rules
=
append
(
rules
,
LogRule
{
PathScope
:
args
[
0
],
OutputFile
:
args
[
1
],
Format
:
format
,
})
}
}
}
logger
=
log
.
New
(
file
,
""
,
0
)
return
rules
,
nil
return
nil
}
})
return
func
(
next
http
.
HandlerFunc
)
http
.
HandlerFunc
{
type
Logger
struct
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
Next
middleware
.
HandlerFunc
sw
:=
middleware
.
NewResponseRecorder
(
w
)
Rules
[]
LogRule
next
(
sw
,
r
)
}
rep
:=
middleware
.
NewReplacer
(
r
,
sw
)
logger
.
Println
(
rep
.
Replace
(
format
))
type
LogRule
struct
{
}
PathScope
string
},
nil
OutputFile
string
Format
string
Log
*
log
.
Logger
}
}
const
(
const
(
defaultLogFilename
=
"access.log"
defaultLogFilename
=
"access.log"
commonLogFormat
=
`{remote} `
+
middleware
.
EmptyStringReplacer
+
` [{when}] "{method} {uri} {proto}" {status} {size}`
commonLogFormat
=
`{remote} `
+
middleware
.
EmptyStringReplacer
+
` [{when}] "{method} {uri} {proto}" {status} {size}`
combinedLogFormat
=
commonLogFormat
+
` "{>Referer}" "{>User-Agent}"`
combinedLogFormat
=
commonLogFormat
+
` "{>Referer}" "{>User-Agent}"`
default
ReqLogFormat
=
commonLogFormat
default
LogFormat
=
commonLogFormat
)
)
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