-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Access Logging on the user server
Co-authored-by: Roberto Abdelkader Martínez Pérez <robertomartinezp@gmail.com>
- Loading branch information
1 parent
41be4f9
commit 2aaf35e
Showing
7 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,45 @@ | ||
package logger | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"time" | ||
) | ||
|
||
var A *log.Logger | ||
var L *log.Logger | ||
|
||
func init() { | ||
A = log.New(os.Stdout, "", 0) | ||
L = log.New(os.Stderr, "", log.LstdFlags|log.Lmicroseconds|log.LUTC) | ||
} | ||
|
||
// See https://en.wikipedia.org/wiki/Common_Log_Format | ||
// Tested with love against CLFParser https://pypi.org/project/clfparser/ | ||
func LogAccess(clientAddr, handlerId, user, method, resource, version string, status int, bytesSent int64, referer, userAgent string) { | ||
clientAddr = dashIfEmpty(clientAddr) | ||
handlerId = dashIfEmpty(handlerId) | ||
user = dashIfEmpty(user) | ||
referer = dashIfEmpty(referer) | ||
userAgent = dashIfEmpty(userAgent) | ||
firstLine := fmt.Sprintf("%s %s %s", method, resource, version) | ||
ts := time.Now().UTC().Format("02/Jan/2006:15:04:05 -0700") // Amazing date format layout! Not. | ||
A.Printf("%s %s %s [%s] %q %d %d %q %q\n", | ||
clientAddr, | ||
handlerId, | ||
user, | ||
ts, | ||
firstLine, | ||
status, | ||
bytesSent, | ||
referer, | ||
userAgent) | ||
} | ||
|
||
func dashIfEmpty(value string) string { | ||
if value == "" { | ||
return "-" | ||
} | ||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters