From b79c801b3cc3295525fd6aae9834c94ec7a8aaa6 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 2 Oct 2010 18:11:57 -0700 Subject: [PATCH] logging.analysis: fix it up and make it work --- basis/logging/analysis/analysis.factor | 43 ++++++++++++------------ basis/logging/insomniac/insomniac.factor | 8 ++--- basis/logging/logging-tests.factor | 4 ++- basis/logging/parser/parser.factor | 21 ++++++++++-- 4 files changed, 47 insertions(+), 29 deletions(-) diff --git a/basis/logging/analysis/analysis.factor b/basis/logging/analysis/analysis.factor index eb8a2eaf76..786aa77c52 100644 --- a/basis/logging/analysis/analysis.factor +++ b/basis/logging/analysis/analysis.factor @@ -1,8 +1,8 @@ -! Copyright (C) 2008 Slava Pestov. +! Copyright (C) 2008, 2010 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: kernel sequences namespaces words assocs logging sorting prettyprint io io.styles io.files io.encodings.utf8 -strings combinators accessors arrays +strings combinators accessors arrays math logging.server logging.parser calendar.format ; IN: logging.analysis @@ -20,6 +20,9 @@ SYMBOL: message-histogram ] when drop ; +: recent-histogram ( assoc n -- alist ) + [ >alist sort-values ] dip short head ; + : analyze-entries ( entries word-names -- errors word-histogram message-histogram ) [ word-names set @@ -27,44 +30,40 @@ SYMBOL: message-histogram H{ } clone word-histogram set H{ } clone message-histogram set - [ - analyze-entry - ] each + [ analyze-entry ] each errors get - word-histogram get - message-histogram get + word-histogram get 10 recent-histogram + message-histogram get 10 recent-histogram ] with-scope ; : histogram. ( assoc quot -- ) standard-table-style [ - [ >alist sort-values ] dip [ + [ [ swapd with-cell pprint-cell ] with-row ] curry assoc-each ] tabular-output ; inline -: log-entry. ( entry -- ) - "====== " write - { - [ date>> (timestamp>string) bl ] - [ level>> pprint bl ] - [ word-name>> write nl ] - [ message>> "\n" join print ] - } cleave ; +: 10-most-recent ( errors -- errors ) + 10 tail* "Only showing 10 most recent errors" print nl ; : errors. ( errors -- ) - [ log-entry. ] each ; + dup length 10 >= [ 10-most-recent ] when + log-entries. ; : analysis. ( errors word-histogram message-histogram -- ) - "==== INTERESTING MESSAGES:" print nl + nl "==== FREQUENT MESSAGES:" print nl "Total: " write dup values sum . nl [ - dup level>> write ": " write message>> "\n" join write + [ first name>> write bl ] + [ second write ": " write ] + [ third "\n" join write ] + tri ] histogram. - nl - "==== WORDS:" print nl + nl nl + "==== FREQUENT WORDS:" print nl [ write ] histogram. - nl + nl nl "==== ERRORS:" print nl errors. ; diff --git a/basis/logging/insomniac/insomniac.factor b/basis/logging/insomniac/insomniac.factor index 2a0be6aa79..0175fd1424 100644 --- a/basis/logging/insomniac/insomniac.factor +++ b/basis/logging/insomniac/insomniac.factor @@ -1,4 +1,4 @@ -! Copyright (C) 2008 Slava Pestov. +! Copyright (C) 2008, 2010 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: logging.analysis logging.server logging smtp kernel io.files io.streams.string namespaces make timers assocs @@ -14,7 +14,7 @@ SYMBOL: insomniac-recipients : email-subject ( service -- string ) [ - "[INSOMNIAC] " % % " on " % io.sockets:host-name % + "Log analysis for " % % " on " % io.sockets:host-name % ] "" make ; : (email-log-report) ( service word-names -- ) @@ -33,5 +33,5 @@ SYMBOL: insomniac-recipients "logging.insomniac" [ (email-log-report) ] with-logging ; : schedule-insomniac ( service word-names -- ) - [ [ email-log-report ] assoc-each rotate-logs ] 2curry - 1 days delayed-every drop ; + [ email-log-report rotate-logs ] 2curry + 1 days every drop ; diff --git a/basis/logging/logging-tests.factor b/basis/logging/logging-tests.factor index 796c8769fc..a7cc6c6f5f 100644 --- a/basis/logging/logging-tests.factor +++ b/basis/logging/logging-tests.factor @@ -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 diff --git a/basis/logging/parser/parser.factor b/basis/logging/parser/parser.factor index dbc26c7efc..a359c9a254 100644 --- a/basis/logging/parser/parser.factor +++ b/basis/logging/parser/parser.factor @@ -1,9 +1,9 @@ -! Copyright (C) 2008 Slava Pestov. +! Copyright (C) 2008, 2010 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: accessors peg peg.parsers memoize kernel sequences logging arrays words strings vectors io io.files io.encodings.utf8 namespaces make combinators logging.server -calendar calendar.format assocs ; +calendar calendar.format assocs prettyprint ; IN: logging.parser TUPLE: log-entry date level word-name message ; @@ -83,3 +83,20 @@ PEG: parse-log-line ( string -- entry ) 'log-line' ; : parse-log-file ( service -- entries ) log-path 1 log# dup exists? [ utf8 file-lines parse-log ] [ drop f ] if ; + +GENERIC: log-timestamp. ( date -- ) + +M: timestamp log-timestamp. (timestamp>string) ; +M: word log-timestamp. drop "multiline" write ; + +: log-entry. ( entry -- ) + "====== " write + { + [ date>> log-timestamp. bl ] + [ level>> pprint bl ] + [ word-name>> write nl ] + [ message>> "\n" join print ] + } cleave ; + +: log-entries. ( errors -- ) + [ log-entry. ] each ;