Application Logging Improvement – Part 3 Making it Readable

This is part three of my Application Logging improvement plan. So far I have discussed that log should be machine readable for application performance, management and monitoring. In this post I give an example of how to make the log readable to human (or make the log just like everyone has been used to seeing them). I am going to use vim to view the log files and have it configured so it knows how to handle the file with syntax etc.

First thing is configure vim to recognize the format. We create that in ftdetect directory. I use my home directory to store all of this but it can be moved to system directory or another directory for application’s usage and sourced it from there. Lets say our log files have “.mylog” as extension. So we create .vim/ftdetect/mylog.vim with following content.

au BufRead,BufNewFile *.mylog set filetype=mylog

Next is to create syntax file which will change log visually to make it more readable. By coloring errors or fatal messages in red it will keep the focus of viewer. Other things can go in different colors. So we create .vim/syntax/mylog.vim with following content. This will give some idea on what is going on and can be updated to suite ones taste.

" Vim syntax file
" Language: My Log Processor
" Maintainer: Vishal Jain
" Latest Revision: 24 September 2014</code>

if exists("b:current_syntax")
    finish
endif

"syn region logLineRegion start="1411" end="$" contains=IndicatorStatus,IndicatorDebug,logTime,IndicatorError,String fold transparent keepend

syn match String /="[^"]*"&amp;/hs=s+1,he=e-1
syn match IndicatorStatus /^S .*$/
syn match IndicatorError /^E .*$/
syn match IndicatorFatal /^F .*$/
syn match IndicatorDebug /^D .*$/

hi def link IndicatorDebug Comment
hi def link IndicatorStatus Type
hi def link IndicatorError Error
hi def link IndicatorFatal Error
hi def link logTime Comment
hi def link String Error

" Convert unixtime to human readable time
function ConvertLog()
    silent! %!perl -pe 'use POSIX 'strftime'; s@(\d{10})@strftime("\%F \%T", localtime($1)) ."."@e;'
endfunction

call ConvertLog()

let b:current_sytax="mylog"

That’s it folks.