Commit 372beaf62734908b8eec30fdc1dd7f76774ffee6

Authored by Marko Tikvić
1 parent dda53db017
Exists in master

refactored if statements

Showing 1 changed file with 86 additions and 73 deletions   Show diff stats
... ... @@ -64,103 +64,116 @@ func (l *Logger) Print(format string, v ...interface{}) {
64 64 }
65 65  
66 66 func (l *Logger) Log(format string, v ...interface{}) {
67   - if l.outputFile != nil {
68   - l.mu.Lock()
69   - defer l.mu.Unlock()
70   -
71   - msg := fmt.Sprintf(format, v...)
72   - s := time.Now().Format(time.RFC3339) + ": " + msg + "\n"
73   - if l.shouldSplit(len(s)) {
74   - l.split()
75   - }
76   - l.outputFile.WriteString(s)
  67 + if l.outputFile == nil {
  68 + return
  69 + }
  70 +
  71 + l.mu.Lock()
  72 + defer l.mu.Unlock()
  73 +
  74 + msg := fmt.Sprintf(format, v...)
  75 + s := time.Now().Format(time.RFC3339) + ": " + msg + "\n"
  76 + if l.shouldSplit(len(s)) {
  77 + l.split()
77 78 }
  79 + l.outputFile.WriteString(s)
78 80 }
79 81  
80   -func (l *Logger) LogRequest(req *http.Request, userid string) {
81   - if l.outputFile != nil {
82   - if userid == "" {
83   - userid = "-"
84   - }
  82 +func (l *Logger) RequestLog(req *http.Request, userid string) string {
  83 + if l.outputFile == nil {
  84 + return ""
  85 + }
85 86  
86   - var b strings.Builder
87   - b.WriteString("Request:\n")
88   - // CLF-like header
89   - ts := time.Now().Format(time.RFC3339)
90   - fmt.Fprintf(&b, "%s %s\n", req.RemoteAddr, ts)
91   -
92   - body, err := httputil.DumpRequest(req, true)
93   - if err != nil {
94   - fmt.Fprintf(os.Stderr, "%v\n", err)
95   - } else {
96   - b.WriteString(string(body))
97   - }
98   - b.WriteString("\n")
  87 + if userid == "" {
  88 + userid = "-"
  89 + }
99 90  
100   - msg := b.String()
  91 + var b strings.Builder
  92 + b.WriteString("Request:\n")
  93 + // CLF-like header
  94 + ts := time.Now().Format(time.RFC3339)
  95 + fmt.Fprintf(&b, "%s %s\n", req.RemoteAddr, ts)
101 96  
102   - l.mu.Lock()
103   - defer l.mu.Unlock()
  97 + body, err := httputil.DumpRequest(req, true)
  98 + if err != nil {
  99 + fmt.Fprintf(os.Stderr, "%v\n", err)
  100 + } else {
  101 + b.WriteString(string(body))
  102 + }
  103 + b.WriteString("\n\n")
104 104  
105   - if l.shouldSplit(len(msg)) {
106   - l.split()
107   - }
108   - l.outputFile.WriteString(msg)
  105 + msg := b.String()
  106 +
  107 + return msg
  108 +}
  109 +
  110 +func (l *Logger) ResponseLog(status int, duration time.Duration) string {
  111 + if l.outputFile == nil {
  112 + return ""
109 113 }
  114 +
  115 + var b strings.Builder
  116 + fmt.Fprintf(&b, "Response: %d %v\n", status, duration)
  117 + b.WriteString("==============================================================\n\n")
  118 + msg := b.String()
  119 +
  120 + return msg
110 121 }
111 122  
112   -func (l *Logger) LogResponse(w http.ResponseWriter, req *http.Request, duration time.Duration) {
113   - if l.outputFile != nil {
114   - var b strings.Builder
115   - b.WriteString("Response:\n")
116   - fmt.Fprintf(&b, "%s %s\n", req.Method, req.RequestURI)
117   - for k, v := range w.Header() {
118   - fmt.Fprintf(&b, "%s: %s\n", k, v)
119   - }
120   - fmt.Fprintf(&b, "\nCompleted in: %v\n", duration)
121   - b.WriteString("==============================================================\n\n")
122   - msg := b.String()
  123 +func (l *Logger) LogHTTPTraffic(in string, out string) {
  124 + if l.outputFile == nil {
  125 + return
  126 + }
123 127  
124   - l.mu.Lock()
125   - defer l.mu.Unlock()
  128 + msg := in + out
126 129  
127   - if l.shouldSplit(len(msg)) {
128   - l.split()
129   - }
130   - l.outputFile.WriteString(msg)
  130 + l.mu.Lock()
  131 + defer l.mu.Unlock()
  132 +
  133 + if l.shouldSplit(len(msg)) {
  134 + l.split()
131 135 }
  136 + l.outputFile.WriteString(msg)
132 137 }
133 138  
134 139 func (l *Logger) Trace(format string, v ...interface{}) {
135   - if l.outputFile != nil {
136   - l.mu.Lock()
137   - defer l.mu.Unlock()
138   - _, file, line, ok := runtime.Caller(1)
139   -
140   - s := ""
141   - msg := fmt.Sprintf(format, v...)
142   - if ok {
143   - s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg)
144   - } else {
145   - s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n")
146   - }
147   - if l.shouldSplit(len(s)) {
148   - l.split()
149   - }
150   - l.outputFile.WriteString(s)
  140 + if l.outputFile == nil {
  141 + return
  142 + }
  143 +
  144 + l.mu.Lock()
  145 + defer l.mu.Unlock()
  146 + _, file, line, ok := runtime.Caller(1)
  147 +
  148 + s := ""
  149 + msg := fmt.Sprintf(format, v...)
  150 + if ok {
  151 + s = fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg)
  152 + } else {
  153 + s = fmt.Sprintf(time.Now().Format(time.RFC3339) + ": [can't retreive stack details]:" + msg + "\n")
151 154 }
  155 + if l.shouldSplit(len(s)) {
  156 + l.split()
  157 + }
  158 + l.outputFile.WriteString(s)
152 159 }
153 160  
154 161 func (l *Logger) Close() {
155   - if l.outputFile != nil {
156   - err := l.outputFile.Close()
157   - if err != nil {
158   - fmt.Printf("logger: on exit: couldn't close event log file\n")
159   - }
  162 + if l.outputFile == nil {
  163 + return
  164 + }
  165 +
  166 + err := l.outputFile.Close()
  167 + if err != nil {
  168 + fmt.Printf("logger: on exit: couldn't close event log file\n")
160 169 }
161 170 }
162 171  
163 172 func (l *Logger) split() {
  173 + if l.outputFile == nil {
  174 + return
  175 + }
  176 +
164 177 // close old file
165 178 err := l.outputFile.Close()
166 179 if err != nil {
... ...