From 71aa99a5e3f18dec13c217afbb38479089deafe4 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Mon, 22 Jun 2015 17:38:55 +0200 Subject: [PATCH] Implemented the logging viewer --- Admin/Assets/CSSAdmin.go | 8 + Admin/Scheme/LogEvent.go | 8 + Admin/Scheme/LoggingViewer.go | 73 ++++++++ Admin/Scheme/Scheme.go | 25 --- Admin/Templates/LoggingViewer.go | 174 +++++++++++-------- Log/DeviceDatabase/ReadMessageNames.go | 12 +- Log/DeviceDatabase/ReadMessageNamesFromDB.go | 10 +- Log/DeviceDatabase/ReadSenderNames.go | 12 +- Log/DeviceDatabase/ReadSenderNamesFromDB.go | 9 +- Log/DeviceDatabase/Variables.go | 27 +-- Log/Web/HandlerLog.go | 12 +- 11 files changed, 250 insertions(+), 120 deletions(-) create mode 100644 Admin/Scheme/LogEvent.go create mode 100644 Admin/Scheme/LoggingViewer.go delete mode 100644 Admin/Scheme/Scheme.go diff --git a/Admin/Assets/CSSAdmin.go b/Admin/Assets/CSSAdmin.go index a4884b9..abfc0ba 100644 --- a/Admin/Assets/CSSAdmin.go +++ b/Admin/Assets/CSSAdmin.go @@ -259,6 +259,14 @@ p { .adminitemlog { padding-top: 14px; } +.columns { + padding-right: 10px; + padding-left: 10px; + text-align: left; +} +.filterformcontainer { + text-align: center; +} @media (max-width: 991px) { .icons.oneback { margin-left: 113px; diff --git a/Admin/Scheme/LogEvent.go b/Admin/Scheme/LogEvent.go new file mode 100644 index 0000000..27c54e9 --- /dev/null +++ b/Admin/Scheme/LogEvent.go @@ -0,0 +1,8 @@ +package Scheme + +// Type for a log event +type LogEvent struct { + LogLine string + LogLevel string // logwarn || logdebug || logerror || loginfo || logtalkative || logsecurity + AB string // loga || logb +} diff --git a/Admin/Scheme/LoggingViewer.go b/Admin/Scheme/LoggingViewer.go new file mode 100644 index 0000000..576ca32 --- /dev/null +++ b/Admin/Scheme/LoggingViewer.go @@ -0,0 +1,73 @@ +package Scheme + +import ( + "html/template" +) + +// The type for the web logger viewer template +type LoggingViewer struct { + Title string + SetLiveView bool + CurrentLevel string + CurrentTimeRange string + CurrentCategory string + CurrentImpact string + CurrentSeverity string + CurrentMessageName string + CurrentSender string + CurrentPage string + MessageNames []MessageNames + Sender []Sender + Events []LogEvent +} + +// The type for the message names is necessary to be able to define a function on it. +type MessageNames string + +// The type for the senders is necessary to be able to define a function on it. +type Sender string + +// This function is used from the template to mark selected values. This is for the type MessageNames. +func (lv MessageNames) IsSelected(field MessageNames, currentValue string) string { + if string(field) == currentValue { + return ` selected` + } else { + return `` + } +} + +// This function is necessary to mark the HTML attribute as safe. Only then it is possible +// to change plain HTML code. +func (lv MessageNames) Safe(element string) template.HTMLAttr { + return template.HTMLAttr(element) +} + +// This function is used from the template to mark selected values. This is for the type Sender. +func (lv Sender) IsSelected(field Sender, currentValue string) string { + if string(field) == currentValue { + return ` selected` + } else { + return `` + } +} + +// This function is necessary to mark the HTML attribute as safe. Only then it is possible +// to change plain HTML code. +func (lv Sender) Safe(element string) template.HTMLAttr { + return template.HTMLAttr(element) +} + +// This function is used from the template to mark selected values. +func (lv LoggingViewer) IsSelected(field, currentValue string) string { + if field == currentValue { + return ` selected` + } else { + return `` + } +} + +// This function is necessary to mark the HTML attribute as safe. Only then it is possible +// to change plain HTML code. +func (lv LoggingViewer) Safe(element string) template.HTMLAttr { + return template.HTMLAttr(element) +} diff --git a/Admin/Scheme/Scheme.go b/Admin/Scheme/Scheme.go deleted file mode 100644 index 1a6b043..0000000 --- a/Admin/Scheme/Scheme.go +++ /dev/null @@ -1,25 +0,0 @@ -package Scheme - -// The type for the web logger viewer template -type Viewer struct { - Title string - SetLiveView bool - CurrentLevel string - CurrentTimeRange string - CurrentCategory string - CurrentImpact string - CurrentSeverity string - CurrentMessageName string - CurrentSender string - CurrentPage string - MessageNames []string - Sender []string - Events []LogEvent -} - -// Type for a log event -type LogEvent struct { - LogLine string - LogLevel string // logwarn || logdebug || logerror || loginfo || logtalkative || logsecurity - AB string // loga || logb -} diff --git a/Admin/Templates/LoggingViewer.go b/Admin/Templates/LoggingViewer.go index 68a3f5f..a0cbda5 100644 --- a/Admin/Templates/LoggingViewer.go +++ b/Admin/Templates/LoggingViewer.go @@ -11,7 +11,7 @@ var LoggingViewer string = ` {{.Title}} {{if .SetLiveView}} - + {{end}} @@ -24,91 +24,117 @@ var LoggingViewer string = `

Logging Viewer

-
+

Filtering

-
- -
-

By Filter

-
-
- - - - - - - - - - - - - -
+
+
+
+
+
+

By Time Range

+ + +
+
+
+
+

By Filter

+ + + + + + + + + + + + +
+
-
+ + +
+
+

Thank you! Your submission has been received!

+
+
+

Oops! Something went wrong while submitting the form :(

-
+

Output

    {{range .Events}} @@ -128,6 +154,12 @@ var LoggingViewer string = `
    +
    +

    Thank you! Your submission has been received!

    +
    +
    +

    Oops! Something went wrong while submitting the form :(

    +
+1Last
diff --git a/Log/DeviceDatabase/ReadMessageNames.go b/Log/DeviceDatabase/ReadMessageNames.go index 8bf77c5..cfb7a55 100644 --- a/Log/DeviceDatabase/ReadMessageNames.go +++ b/Log/DeviceDatabase/ReadMessageNames.go @@ -1,9 +1,17 @@ package DeviceDatabase +import ( + "github.com/SommerEngineering/Ocean/Admin/Scheme" +) + // Read the message names out of the cache. -func ReadMessageNames() (messageNames []string) { +func ReadMessageNames() (messageNames []Scheme.MessageNames) { mutexCacheMessageNames.RLock() defer mutexCacheMessageNames.RUnlock() - messageNames = cacheMessageNames + + // Transform the values to the right format: + for _, entry := range cacheMessageNames { + messageNames = append(messageNames, Scheme.MessageNames(entry)) + } return } diff --git a/Log/DeviceDatabase/ReadMessageNamesFromDB.go b/Log/DeviceDatabase/ReadMessageNamesFromDB.go index e0703a8..63e51bd 100644 --- a/Log/DeviceDatabase/ReadMessageNamesFromDB.go +++ b/Log/DeviceDatabase/ReadMessageNamesFromDB.go @@ -1,6 +1,7 @@ package DeviceDatabase import ( + "github.com/SommerEngineering/Ocean/Admin/Scheme" "github.com/SommerEngineering/Ocean/Log" LM "github.com/SommerEngineering/Ocean/Log/Meta" "gopkg.in/mgo.v2/bson" @@ -8,7 +9,7 @@ import ( ) // Read the message names from the database without any cache. -func readMessageNamesFromDB() (result []string) { +func readMessageNamesFromDB() (result []Scheme.MessageNames) { var nextMessageNames []string if err := logDBCollection.Find(bson.D{}).Distinct(`MessageName`, &nextMessageNames); err != nil { // Case: Error, was not able to write the event to the database: @@ -18,6 +19,11 @@ func readMessageNamesFromDB() (result []string) { // Sort the sender names: sort.Strings(nextMessageNames) - result = nextMessageNames + + // Transform the values to the right format: + for _, entry := range nextMessageNames { + result = append(result, Scheme.MessageNames(entry)) + } + return } diff --git a/Log/DeviceDatabase/ReadSenderNames.go b/Log/DeviceDatabase/ReadSenderNames.go index 296b564..409d0b0 100644 --- a/Log/DeviceDatabase/ReadSenderNames.go +++ b/Log/DeviceDatabase/ReadSenderNames.go @@ -1,9 +1,17 @@ package DeviceDatabase +import ( + "github.com/SommerEngineering/Ocean/Admin/Scheme" +) + // Read the sender names out of the cache. -func ReadSenderNames() (senderNames []string) { +func ReadSenderNames() (senderNames []Scheme.Sender) { mutexCacheSenderNames.RLock() defer mutexCacheSenderNames.RUnlock() - senderNames = cacheSenderNames + + // Transform the values to the right format: + for _, entry := range cacheSenderNames { + senderNames = append(senderNames, Scheme.Sender(entry)) + } return } diff --git a/Log/DeviceDatabase/ReadSenderNamesFromDB.go b/Log/DeviceDatabase/ReadSenderNamesFromDB.go index 321d2e4..3279d9d 100644 --- a/Log/DeviceDatabase/ReadSenderNamesFromDB.go +++ b/Log/DeviceDatabase/ReadSenderNamesFromDB.go @@ -1,6 +1,7 @@ package DeviceDatabase import ( + "github.com/SommerEngineering/Ocean/Admin/Scheme" "github.com/SommerEngineering/Ocean/Log" LM "github.com/SommerEngineering/Ocean/Log/Meta" "gopkg.in/mgo.v2/bson" @@ -8,7 +9,7 @@ import ( ) // Reads the sender names from the database without any caching. -func readSenderNamesFromDB() (result []string) { +func readSenderNamesFromDB() (result []Scheme.Sender) { var nextSenderNames []string if err := logDBCollection.Find(bson.D{}).Distinct(`Sender`, &nextSenderNames); err != nil { // Case: Was not possible to write to the database. @@ -18,6 +19,10 @@ func readSenderNamesFromDB() (result []string) { // Sort the sender names: sort.Strings(nextSenderNames) - result = nextSenderNames + + // Transform the values to the right format: + for _, entry := range nextSenderNames { + result = append(result, Scheme.Sender(entry)) + } return } diff --git a/Log/DeviceDatabase/Variables.go b/Log/DeviceDatabase/Variables.go index 62d34fe..acd0914 100644 --- a/Log/DeviceDatabase/Variables.go +++ b/Log/DeviceDatabase/Variables.go @@ -1,6 +1,7 @@ package DeviceDatabase import ( + "github.com/SommerEngineering/Ocean/Admin/Scheme" LM "github.com/SommerEngineering/Ocean/Log/Meta" "gopkg.in/mgo.v2" "sync" @@ -8,17 +9,17 @@ import ( var ( // This is the name for logging event from this package: - senderName LM.Sender = `System::Logger::Database` - mutexCacheFull sync.Mutex = sync.Mutex{} - mutexCacheSenderNames sync.RWMutex = sync.RWMutex{} - mutexCacheMessageNames sync.RWMutex = sync.RWMutex{} - cache chan LogDBEntry = nil - cacheSizeNumberOfEvents int = 50 - cacheSizeTime2FlushSeconds int = 6 - nameCachesRefreshTimeSeconds int = 300 - cacheSenderNames []string = nil - cacheMessageNames []string = nil - logDB *mgo.Database = nil - logDBSession *mgo.Session = nil - logDBCollection *mgo.Collection = nil + senderName LM.Sender = `System::Logger::Database` + mutexCacheFull sync.Mutex = sync.Mutex{} + mutexCacheSenderNames sync.RWMutex = sync.RWMutex{} + mutexCacheMessageNames sync.RWMutex = sync.RWMutex{} + cache chan LogDBEntry = nil + cacheSizeNumberOfEvents int = 50 + cacheSizeTime2FlushSeconds int = 6 + nameCachesRefreshTimeSeconds int = 300 + cacheSenderNames []Scheme.Sender = nil + cacheMessageNames []Scheme.MessageNames = nil + logDB *mgo.Database = nil + logDBSession *mgo.Session = nil + logDBCollection *mgo.Collection = nil ) diff --git a/Log/Web/HandlerLog.go b/Log/Web/HandlerLog.go index 493866f..086927d 100644 --- a/Log/Web/HandlerLog.go +++ b/Log/Web/HandlerLog.go @@ -26,20 +26,20 @@ func HandlerWebLog(response http.ResponseWriter, request *http.Request) { countParameters := len(request.Form) // Setup the data for the HTML template: - data := Scheme.Viewer{} + data := Scheme.LoggingViewer{} data.Title = `Logging Viewer` data.Sender = DeviceDatabase.ReadSenderNames() data.MessageNames = DeviceDatabase.ReadMessageNames() // To less parameters? if countParameters < 9 { - // Initial view => refresh & first page (latest logs) + // Initial view => first page (latest logs) data.Events = readLatest() data.SetLiveView = true } else { // Case: Custom view currentLevel := request.FormValue(`Level`) - currentTimeRange := request.FormValue(`timeRange`) + currentTimeRange := request.FormValue(`TimeRange`) currentCategory := request.FormValue(`Category`) currentImpact := request.FormValue(`Impact`) currentSeverity := request.FormValue(`Severity`) @@ -96,6 +96,12 @@ func HandlerWebLog(response http.ResponseWriter, request *http.Request) { } else { data.CurrentSender = `*` } + + if currentPage != `` { + data.CurrentPage = currentPage + } else { + data.CurrentPage = `*` + } } // Write the MIME type and execute the template: