Commit df74f4a7ee0b8df78da5b96abc0749b19854ddd0

Authored by Marko Tikvić
1 parent aa60a45eec
Exists in master

new filename format

Showing 1 changed file with 61 additions and 34 deletions   Show diff stats
... ... @@ -2,6 +2,8 @@ package gologger
2 2  
3 3 import (
4 4 "fmt"
  5 + "io/ioutil"
  6 + "net/http"
5 7 "os"
6 8 "path/filepath"
7 9 "runtime"
... ... @@ -25,6 +27,8 @@ type Logger struct {
25 27  
26 28 outputFileName string
27 29 maxFileSize int64
  30 +
  31 + splitCount int
28 32 }
29 33  
30 34 func New(name string, maxFileSize int64) (logger *Logger, err error) {
... ... @@ -42,9 +46,9 @@ func New(name string, maxFileSize int64) (logger *Logger, err error) {
42 46 }
43 47 }
44 48  
45   - date := strings.Split(time.Now().Format(time.RFC3339), "T")[0]
46   - timestamp := "_" + date
47   - path := filepath.Join(logDirName, logger.outputFileName+timestamp+".txt")
  49 + date := strings.Replace(time.Now().Format(time.RFC3339), ":", ".", -1)
  50 + logger.outputFileName += "_" + date + ".txt"
  51 + path := filepath.Join(logDirName, logger.outputFileName)
48 52 logger.outputFile, err = os.Create(path)
49 53 if err != nil {
50 54 fmt.Printf("logger: new: couldn't create event log file\n")
... ... @@ -54,45 +58,67 @@ func New(name string, maxFileSize int64) (logger *Logger, err error) {
54 58 return logger, nil
55 59 }
56 60  
57   -func (logger *Logger) Print(format string, v ...interface{}) {
  61 +func (l *Logger) Print(format string, v ...interface{}) {
58 62 msg := fmt.Sprintf(format, v...)
59 63 fmt.Printf(time.Now().Format(time.RFC3339) + ": " + msg + "\n")
60 64 }
61 65  
62   -func (logger *Logger) Log(format string, v ...interface{}) {
63   - if logger.outputFile != nil {
64   - logger.mu.Lock()
65   - defer logger.mu.Unlock()
  66 +func (l *Logger) Log(format string, v ...interface{}) {
  67 + if l.outputFile != nil {
  68 + l.mu.Lock()
  69 + defer l.mu.Unlock()
66 70  
67 71 msg := fmt.Sprintf(format, v...)
68 72 s := time.Now().Format(time.RFC3339) + ": " + msg + "\n"
69   - if logger.shouldSplit(len(s)) {
70   - logger.split()
  73 + if l.shouldSplit(len(s)) {
  74 + l.split()
71 75 }
72   - logger.outputFile.WriteString(s)
  76 + l.outputFile.WriteString(s)
73 77 }
74 78 }
75 79  
76   -// TODO(marko)
77   -/*
78   -func (logger *Logger) LogHTTPRequest(req *http.Request) {
79   - if logger.outputFile != nil {
80   - logger.mu.Lock()
81   - defer logger.mu.Unlock()
  80 +func (l *Logger) LogRequest(req *http.Request, userid string) {
  81 + if l.outputFile != nil {
  82 + if userid == "" {
  83 + userid = "-"
  84 + }
82 85  
83   - msg := fmt.Sprintf(format, v...)
84   - if logger.shouldSplit(len(s)) {
85   - logger.split()
  86 + var b strings.Builder
  87 + // CLF-like header
  88 + ts := time.Now().Format(time.RFC3339)
  89 + fmt.Fprintf(&b, "%s - %s [%s] \"%s %s\" %s\n", req.RemoteAddr, userid, ts, req.Method, req.URL.Path, req.Proto)
  90 +
  91 + b.WriteString("\nURL encoded:\n")
  92 + for k, v := range req.Form {
  93 + fmt.Fprintf(&b, "%s = %s\n", k, v)
  94 + }
  95 +
  96 + b.WriteString("\nBody:\n")
  97 + buf, err := ioutil.ReadAll(req.Body)
  98 + if err == nil {
  99 + b.WriteString(string(buf))
  100 + } else {
  101 + fmt.Fprintf(os.Stderr, "%v\n", err)
  102 + }
  103 + b.WriteString("\n\n")
  104 +
  105 + msg := b.String()
  106 + //fmt.Println(msg)
  107 +
  108 + l.mu.Lock()
  109 + defer l.mu.Unlock()
  110 +
  111 + if l.shouldSplit(len(msg)) {
  112 + l.split()
86 113 }
87   - logger.outputFile.WriteString(time.Now().Format(time.RFC3339) + ": " + msg + "\n")
  114 + l.outputFile.WriteString(msg)
88 115 }
89 116 }
90   -*/
91 117  
92   -func (logger *Logger) Trace(format string, v ...interface{}) {
93   - if logger.outputFile != nil {
94   - logger.mu.Lock()
95   - defer logger.mu.Unlock()
  118 +func (l *Logger) Trace(format string, v ...interface{}) {
  119 + if l.outputFile != nil {
  120 + l.mu.Lock()
  121 + defer l.mu.Unlock()
96 122 _, file, line, ok := runtime.Caller(1)
97 123  
98 124 s := ""
... ... @@ -102,16 +128,16 @@ func (logger *Logger) Trace(format string, v ...interface{}) {
102 128 } else {
103 129 s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n")
104 130 }
105   - if logger.shouldSplit(len(s)) {
106   - logger.split()
  131 + if l.shouldSplit(len(s)) {
  132 + l.split()
107 133 }
108   - logger.outputFile.WriteString(s)
  134 + l.outputFile.WriteString(s)
109 135 }
110 136 }
111 137  
112   -func (logger *Logger) Close() {
113   - if logger.outputFile != nil {
114   - err := logger.outputFile.Close()
  138 +func (l *Logger) Close() {
  139 + if l.outputFile != nil {
  140 + err := l.outputFile.Close()
115 141 if err != nil {
116 142 fmt.Printf("logger: on exit: couldn't close event log file\n")
117 143 }
... ... @@ -119,16 +145,17 @@ func (logger *Logger) Close() {
119 145 }
120 146  
121 147 func (l *Logger) split() {
122   - timestamp := "_" + time.Now().Format(time.RFC3339)
123 148 // close old file
124 149 err := l.outputFile.Close()
125 150 if err != nil {
126 151 fmt.Printf("logger: split: couldn't close event file\n")
127 152 return
128 153 }
  154 +
  155 + l.splitCount++
129 156 // open new file
130 157 var errnew error
131   - path := filepath.Join(logDirName, l.outputFileName+timestamp)
  158 + path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount))
132 159 l.outputFile, errnew = os.Create(path)
133 160  
134 161 if errnew != nil {
... ...