]> gitweb.factorcode.org Git - factor.git/commitdiff
logging.analysis: fix it up and make it work
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 3 Oct 2010 01:11:57 +0000 (18:11 -0700)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 3 Oct 2010 01:11:57 +0000 (18:11 -0700)
basis/logging/analysis/analysis.factor
basis/logging/insomniac/insomniac.factor
basis/logging/logging-tests.factor
basis/logging/parser/parser.factor

index eb8a2eaf76b77c63a5113a7754a8a09478fdb07f..786aa77c52faeae180841cd8640b3944b08b389d 100644 (file)
@@ -1,8 +1,8 @@
-! Copyright (C) 2008 Slava Pestov.\r
+! Copyright (C) 2008, 2010 Slava Pestov.\r
 ! See http://factorcode.org/license.txt for BSD license.\r
 USING: kernel sequences namespaces words assocs logging sorting\r
 prettyprint io io.styles io.files io.encodings.utf8\r
-strings combinators accessors arrays\r
+strings combinators accessors arrays math\r
 logging.server logging.parser calendar.format ;\r
 IN: logging.analysis\r
 \r
@@ -20,6 +20,9 @@ SYMBOL: message-histogram
     ] when\r
     drop ;\r
 \r
+: recent-histogram ( assoc n -- alist )\r
+    [ >alist sort-values <reversed> ] dip short head ;\r
+\r
 : analyze-entries ( entries word-names -- errors word-histogram message-histogram )\r
     [\r
         word-names set\r
@@ -27,44 +30,40 @@ SYMBOL: message-histogram
         H{ } clone word-histogram set\r
         H{ } clone message-histogram set\r
 \r
-        [\r
-            analyze-entry\r
-        ] each\r
+        [ analyze-entry ] each\r
 \r
         errors get\r
-        word-histogram get\r
-        message-histogram get\r
+        word-histogram get 10 recent-histogram\r
+        message-histogram get 10 recent-histogram\r
     ] with-scope ;\r
 \r
 : histogram. ( assoc quot -- )\r
     standard-table-style [\r
-        [ >alist sort-values <reversed> ] dip [\r
+        [\r
             [ swapd with-cell pprint-cell ] with-row\r
         ] curry assoc-each\r
     ] tabular-output ; inline\r
 \r
-: log-entry. ( entry -- )\r
-    "====== " write\r
-    {\r
-        [ date>> (timestamp>string) bl ]\r
-        [ level>> pprint bl ]\r
-        [ word-name>> write nl ]\r
-        [ message>> "\n" join print ]\r
-    } cleave ;\r
+: 10-most-recent ( errors -- errors )\r
+    10 tail* "Only showing 10 most recent errors" print nl ;\r
 \r
 : errors. ( errors -- )\r
-    [ log-entry. ] each ;\r
+    dup length 10 >= [ 10-most-recent ] when\r
+    log-entries. ;\r
 \r
 : analysis. ( errors word-histogram message-histogram -- )\r
-    "==== INTERESTING MESSAGES:" print nl\r
+    nl "==== FREQUENT MESSAGES:" print nl\r
     "Total: " write dup values sum . nl\r
     [\r
-        dup level>> write ": " write message>> "\n" join write\r
+        [ first name>> write bl ]\r
+        [ second write ": " write ]\r
+        [ third "\n" join write ]\r
+        tri\r
     ] histogram.\r
-    nl\r
-    "==== WORDS:" print nl\r
+    nl nl\r
+    "==== FREQUENT WORDS:" print nl\r
     [ write ] histogram.\r
-    nl\r
+    nl nl\r
     "==== ERRORS:" print nl\r
     errors. ;\r
 \r
index 2a0be6aa79ed1fd571c1cf1269fb608ca08abc94..0175fd1424124b721e46913fc94aa94b107c1555 100644 (file)
@@ -1,4 +1,4 @@
-! Copyright (C) 2008 Slava Pestov.\r
+! Copyright (C) 2008, 2010 Slava Pestov.\r
 ! See http://factorcode.org/license.txt for BSD license.\r
 USING: logging.analysis logging.server logging smtp kernel\r
 io.files io.streams.string namespaces make timers assocs\r
@@ -14,7 +14,7 @@ SYMBOL: insomniac-recipients
 \r
 : email-subject ( service -- string )\r
     [\r
-        "[INSOMNIAC] " % % " on " % io.sockets:host-name %\r
+        "Log analysis for " % % " on " % io.sockets:host-name %\r
     ] "" make ;\r
 \r
 : (email-log-report) ( service word-names -- )\r
@@ -33,5 +33,5 @@ SYMBOL: insomniac-recipients
     "logging.insomniac" [ (email-log-report) ] with-logging ;\r
 \r
 : schedule-insomniac ( service word-names -- )\r
-    [ [ email-log-report ] assoc-each rotate-logs ] 2curry\r
-    1 days delayed-every drop ;\r
+    [ email-log-report rotate-logs ] 2curry\r
+    1 days every drop ;\r
index 796c8769fc295febb25e4c7a84dddb41bce993fa..a7cc6c6f5f6d2e2c107ccca3c84e9f5a52b0eb89 100644 (file)
@@ -1,5 +1,5 @@
 IN: logging.tests
-USING: tools.test logging math ;
+USING: tools.test logging logging.analysis io math ;
 
 : input-logging-test ( a b -- c ) + ;
 
@@ -22,3 +22,5 @@ USING: tools.test logging math ;
     
     [ f ] [ 1 0 error-logging-test ] unit-test
 ] with-logging
+
+[ ] [ "logging-test" { "input-logging-test" } analyze-log-file ] unit-test
index dbc26c7efcc31c571ec307f84222a5c67abbb406..a359c9a25476a2d79eb525a7817903a135703e02 100644 (file)
@@ -1,9 +1,9 @@
-! Copyright (C) 2008 Slava Pestov.\r
+! Copyright (C) 2008, 2010 Slava Pestov.\r
 ! See http://factorcode.org/license.txt for BSD license.\r
 USING: accessors peg peg.parsers memoize kernel sequences\r
 logging arrays words strings vectors io io.files\r
 io.encodings.utf8 namespaces make combinators logging.server\r
-calendar calendar.format assocs ;\r
+calendar calendar.format assocs prettyprint ;\r
 IN: logging.parser\r
 \r
 TUPLE: log-entry date level word-name message ;\r
@@ -83,3 +83,20 @@ PEG: parse-log-line ( string -- entry ) 'log-line' ;
 : parse-log-file ( service -- entries )\r
     log-path 1 log# dup exists?\r
     [ utf8 file-lines parse-log ] [ drop f ] if ;\r
+\r
+GENERIC: log-timestamp. ( date -- )\r
+\r
+M: timestamp log-timestamp. (timestamp>string) ;\r
+M: word log-timestamp. drop "multiline" write ;\r
+\r
+: log-entry. ( entry -- )\r
+    "====== " write\r
+    {\r
+        [ date>> log-timestamp. bl ]\r
+        [ level>> pprint bl ]\r
+        [ word-name>> write nl ]\r
+        [ message>> "\n" join print ]\r
+    } cleave ;\r
+\r
+: log-entries. ( errors -- )\r
+    [ log-entry. ] each ;\r