@@ -13,24 +13,32 @@ import (
16+// AuthStatus defines the different types of auth logging that occur
16type AuthStatus string
17type AuthStatus string
19− // Default logging formats
20+ // DefaultStandardLoggingFormat defines the default standard log format
20 DefaultStandardLoggingFormat = "[{{.Timestamp}}] [{{.File}}] {{.Message}}"
21 DefaultStandardLoggingFormat = "[{{.Timestamp}}] [{{.File}}] {{.Message}}"
22+ // DefaultAuthLoggingFormat defines the default auth log format
21− DefaultAuthLoggingFormat = "{{.Client}} - {{.Username}} [{{.Timestamp}}] [{{.Status}}] {{.Message}}"
23+ DefaultAuthLoggingFormat = "{{.Client}} - {{.Username}} [{{.Timestamp}}] [{{.Status}}] {{.Message}}"
24+ // DefaultRequestLoggingFormat defines the default request log format
22− DefaultRequestLoggingFormat = "{{.Client}} - {{.Username}} [{{.Timestamp}}] {{.Host}} {{.RequestMethod}} {{.Upstream}} {{.RequestURI}} {{.Protocol}} {{.UserAgent}} {{.StatusCode}} {{.ResponseSize}} {{.RequestDuration}}"
25+ DefaultRequestLoggingFormat = "{{.Client}} - {{.Username}} [{{.Timestamp}}] {{.Host}} {{.RequestMethod}} {{.Upstream}} {{.RequestURI}} {{.Protocol}} {{.UserAgent}} {{.StatusCode}} {{.ResponseSize}} {{.RequestDuration}}"
24− // Auth statuses
27+ // AuthSuccess indicates that an auth attempt has succeeded explicitly
25 AuthSuccess AuthStatus = "AuthSuccess"
28 AuthSuccess AuthStatus = "AuthSuccess"
29+ // AuthFailure indicates that an auth attempt has failed explicitly
26 AuthFailure AuthStatus = "AuthFailure"
30 AuthFailure AuthStatus = "AuthFailure"
31+ // AuthError indicates that an auth attempt has failed due to an error
27− AuthError AuthStatus = "AuthError"
32+ AuthError AuthStatus = "AuthError"
29− // General logging config
30− Llongfile = 1 << iota // full file name and line number: /a/b/c/d.go:23
34+ // Llongfile flag to log full file name and line number: /a/b/c/d.go:23
35+ Llongfile = 1 << iota
31− Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
36+ // Lshortfile flag to log final file name element and line number: d.go:23. overrides Llongfile
32− LUTC // use UTC rather than the local time zone
38+ // LUTC flag to log UTC datetime rather than the local time zone
33− LstdFlags = Lshortfile // initial values for the standard logger
40+ // LstdFlags flag for initial values for the logger
41+ LstdFlags = Lshortfile
36// These are the containers for all values that are available as variables in the logging formats.
44// These are the containers for all values that are available as variables in the logging formats.
@@ -68,6 +76,11 @@ type reqLogMessageData struct {
68 Username string
76 Username string
79+// A Logger represents an active logging object that generates lines of
80+// output to an io.Writer passed through a formatter. Each logging
81+// operation makes a single call to the Writer's Write method. A Logger
82+// can be used simultaneously from multiple goroutines; it guarantees to
83+// serialize access to the Writer.
71type Logger struct {
84type Logger struct {
72 mu sync.Mutex
85 mu sync.Mutex
@@ -96,23 +109,6 @@ func New(flag int) *Logger {
97var std = New(LstdFlags)
110var std = New(LstdFlags)
99−// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding.
100−func itoa(buf *[]byte, i int, wid int) {
101− // Assemble decimal in reverse order.
104− for i >= 10 || wid > 1 {
107− b[bp] = byte('0' + i - q*10)
112− b[bp] = byte('0' + i)
113− *buf = append(*buf, b[bp:]...)
116// Output a standard log template with a simple message.
112// Output a standard log template with a simple message.
117// Write a final newline at the end of every message.
113// Write a final newline at the end of every message.
118func (l *Logger) Output(calldepth int, message string) {
114func (l *Logger) Output(calldepth int, message string) {
@@ -139,9 +135,9 @@ func (l *Logger) Output(calldepth int, message string) {
139 l.writer.Write([]byte("\n"))
135 l.writer.Write([]byte("\n"))
142−// Print auth details to the logger. Requires an http.Request to log details.
138+// PrintAuth writes auth info to the logger. Requires an http.Request to
143−// Remaining arguments are handled in the manner of fmt.Sprintf.
139+// log request details. Remaining arguments are handled in the manner of
144−// Writes a final newline to the end of every message.
140+// fmt.Sprintf. Writes a final newline to the end of every message.
145func (l *Logger) PrintAuth(username string, req *http.Request, status AuthStatus, format string, a ...interface{}) {
141func (l *Logger) PrintAuth(username string, req *http.Request, status AuthStatus, format string, a ...interface{}) {
146 if !l.authEnabled {
142 if !l.authEnabled {
@@ -173,8 +169,9 @@ func (l *Logger) PrintAuth(username string, req *http.Request, status AuthStatus
173 l.writer.Write([]byte("\n"))
169 l.writer.Write([]byte("\n"))
176−// Print HTTP request details to the Logger.
172+// PrintReq writes request details to the Logger using the http.Request,
177−// Writes a final newline to the end of every message.
173+// url, and timestamp of the request. Writes a final newline to the end
178func (l *Logger) PrintReq(username, upstream string, req *http.Request, url url.URL, ts time.Time, status int, size int) {
175func (l *Logger) PrintReq(username, upstream string, req *http.Request, url url.URL, ts time.Time, status int, size int) {
179 if !l.reqEnabled {
176 if !l.reqEnabled {
@@ -219,7 +216,9 @@ func (l *Logger) PrintReq(username, upstream string, req *http.Request, url url.
219 l.writer.Write([]byte("\n"))
216 l.writer.Write([]byte("\n"))
222−// Obtains file and line number info for the standard output log
219+// GetFileLineString will find the caller file and line number
220+// taking in to account the calldepth to iterate up the stack
221+// to find the non-logging call location.
223func (l *Logger) GetFileLineString(calldepth int) string {
222func (l *Logger) GetFileLineString(calldepth int) string {
224 var file string
223 var file string
225 var line int
224 var line int
@@ -245,7 +244,7 @@ func (l *Logger) GetFileLineString(calldepth int) string {
245 return fmt.Sprintf("%s:%d", file, line)
244 return fmt.Sprintf("%s:%d", file, line)
248−// Parse an HTTP request for the client/remote IP
247+// GetClient parses an HTTP request for the client/remote IP address.
249func GetClient(req *http.Request) string {
248func GetClient(req *http.Request) string {
250 client := req.Header.Get("X-Real-IP")
249 client := req.Header.Get("X-Real-IP")
251 if client == "" {
250 if client == "" {
@@ -259,7 +258,7 @@ func GetClient(req *http.Request) string {
259 return client
258 return client
262−// Returns a formatted timestamp
261+// FormatTimestamp returns a formatted timestamp.
263func (l *Logger) FormatTimestamp(ts time.Time) string {
262func (l *Logger) FormatTimestamp(ts time.Time) string {
264 if l.flag&LUTC != 0 {
263 if l.flag&LUTC != 0 {
265 ts = ts.UTC()
264 ts = ts.UTC()
@@ -282,42 +281,42 @@ func (l *Logger) SetFlags(flag int) {
282 l.flag = flag
281 l.flag = flag
285−// Set all logging enabled for the logger.
284+// SetStandardEnabled enables or disables standard logging.
286func (l *Logger) SetStandardEnabled(e bool) {
285func (l *Logger) SetStandardEnabled(e bool) {
287 l.mu.Lock()
286 l.mu.Lock()
288 defer l.mu.Unlock()
287 defer l.mu.Unlock()
289 l.stdEnabled = e
288 l.stdEnabled = e
292−// Set auth logging enabled for the standard logger
291+// SetAuthEnabled enables or disables auth logging.
293func (l *Logger) SetAuthEnabled(e bool) {
292func (l *Logger) SetAuthEnabled(e bool) {
294 l.mu.Lock()
293 l.mu.Lock()
295 defer l.mu.Unlock()
294 defer l.mu.Unlock()
296 l.authEnabled = e
295 l.authEnabled = e
299−// Set request logging enabled for the standard logger
298+// SetReqEnabled enabled or disables request logging.
300func (l *Logger) SetReqEnabled(e bool) {
299func (l *Logger) SetReqEnabled(e bool) {
301 l.mu.Lock()
300 l.mu.Lock()
302 defer l.mu.Unlock()
301 defer l.mu.Unlock()
303 l.reqEnabled = e
302 l.reqEnabled = e
306−// Set the output template for the standard logger
305+// SetStandardTemplate sets the template for standard logging.
307func (l *Logger) SetStandardTemplate(t string) {
306func (l *Logger) SetStandardTemplate(t string) {
308 l.mu.Lock()
307 l.mu.Lock()
309 defer l.mu.Unlock()
308 defer l.mu.Unlock()
310 l.stdLogTemplate = template.Must(template.New("std-log").Parse(t))
309 l.stdLogTemplate = template.Must(template.New("std-log").Parse(t))
313−// Set the auth template for the standard logger
312+// SetAuthTemplate sets the template for auth logging.
314func (l *Logger) SetAuthTemplate(t string) {
313func (l *Logger) SetAuthTemplate(t string) {
315 l.mu.Lock()
314 l.mu.Lock()
316 defer l.mu.Unlock()
315 defer l.mu.Unlock()
317 l.authTemplate = template.Must(template.New("auth-log").Parse(t))
316 l.authTemplate = template.Must(template.New("auth-log").Parse(t))
320−// Set the request template for the standard logger
319+// SetReqTemplate sets the template for request logging.
321func (l *Logger) SetReqTemplate(t string) {
320func (l *Logger) SetReqTemplate(t string) {
322 l.mu.Lock()
321 l.mu.Lock()
323 defer l.mu.Unlock()
322 defer l.mu.Unlock()
@@ -326,7 +325,7 @@ func (l *Logger) SetReqTemplate(t string) {
327// These functions utilize the standard logger.
326// These functions utilize the standard logger.
329−// Returns a formatted timestamp for the standard logger.
328+// FormatTimestamp returns a formatted timestamp for the standard logger.
330func FormatTimestamp(ts time.Time) string {
329func FormatTimestamp(ts time.Time) string {
331 return std.FormatTimestamp(ts)
330 return std.FormatTimestamp(ts)
@@ -348,32 +347,38 @@ func SetOutput(w io.Writer) {
348 std.writer = w
347 std.writer = w
350+// SetStandardEnabled enables or disables standard logging for the
351−// Set output logging enabled for the standard logger
351+// standard logger.
352func SetStandardEnabled(e bool) {
352func SetStandardEnabled(e bool) {
353 std.SetStandardEnabled(e)
353 std.SetStandardEnabled(e)
356−// Set auth logging enabled for the standard logger
356+// SetAuthEnabled enables or disables auth logging for the standard
357func SetAuthEnabled(e bool) {
358func SetAuthEnabled(e bool) {
358 std.SetAuthEnabled(e)
359 std.SetAuthEnabled(e)
362+// SetReqEnabled enables or disables request logging for the
361−// Set request logging enabled for the standard logger
363+// standard logger.
362func SetReqEnabled(e bool) {
364func SetReqEnabled(e bool) {
363 std.SetReqEnabled(e)
365 std.SetReqEnabled(e)
368+// SetStandardTemplate sets the template for standard logging for
366−// Set the output template for the standard logger
369+// the standard logger.
367func SetStandardTemplate(t string) {
370func SetStandardTemplate(t string) {
368 std.SetStandardTemplate(t)
371 std.SetStandardTemplate(t)
374+// SetAuthTemplate sets the template for auth logging for the
371−// Set the auth template for the standard logger
375+// standard logger.
372func SetAuthTemplate(t string) {
376func SetAuthTemplate(t string) {
373 std.SetAuthTemplate(t)
377 std.SetAuthTemplate(t)
380+// SetReqTemplate sets the template for request logging for the
376−// Set the request template for the standard logger
381+// standard logger.
377func SetReqTemplate(t string) {
382func SetReqTemplate(t string) {
378 std.SetReqTemplate(t)
383 std.SetReqTemplate(t)
@@ -441,7 +446,7 @@ func PrintAuthf(username string, req *http.Request, status AuthStatus, format st
441 std.PrintAuth(username, req, status, format, a...)
446 std.PrintAuth(username, req, status, format, a...)
444−// PrintAuthf writes request details to the standard logger.
449+// PrintReq writes request details to the standard logger.
445func PrintReq(username, upstream string, req *http.Request, url url.URL, ts time.Time, status int, size int) {
450func PrintReq(username, upstream string, req *http.Request, url url.URL, ts time.Time, status int, size int) {
446 std.PrintReq(username, upstream, req, url, ts, status, size)
451 std.PrintReq(username, upstream, req, url, ts, status, size)