Browse code

Formatovanie logov

Cinan Rakosnik authored on 17/03/2013 at 16:52:44
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,26 @@
0
+class ActiveSupport::BufferedLogger
1
+    def formatter=(formatter)
2
+        @log.formatter = formatter
3
+    end
4
+end
5
+
6
+class Formatter
7
+    SEVERITY_TO_COLOR_MAP   = {'DEBUG'=>'33', 'INFO'=>'0;37', 'WARN'=>'36', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37'}
8
+    USE_HUMOROUS_SEVERITIES = false
9
+
10
+    def call(severity, time, progname, msg)
11
+        formatted_severity = sprintf("%s","#{severity}")
12
+
13
+        formatted_time = time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..2].rjust(3)
14
+        color = SEVERITY_TO_COLOR_MAP[severity]
15
+
16
+        if msg.empty?
17
+            "\n"
18
+        else
19
+            "\033[0;37m#{formatted_time}\033[0m [#{formatted_severity}] \033[#{color}m#{msg.strip}\033[0m\n"
20
+        end
21
+    end
22
+
23
+end
24
+
25
+Rails.logger.formatter = Formatter.new
0 26
\ No newline at end of file