Aliaspooryorik
ColdFusion ORM Book

Getting started with LogBox and ColdBox

I've been working on a ColdBox 3 site and decided to give in the in-built logging (with LogBox) a go. I thought I'd do a quick blog post to demonstrate how to use it as I found the docs a little bit confusing. LogBox can be used standalone, whereas when you use it with ColdBox a lot of the work is done for you.

By default ColdBox is configured with logging enabled and outputs to the console. Here's my Coldbox.cfc just to prove that I'm not cheating!

 &qu
ot;c&oqmupootn;en,t
n a m e =s"escsaichoinnEgndKHeaynd"l eerx t e n d=s =&"qcuooltd;bo&xq.usoyts;t,e
m. i n tmeirscseipntgoTre"m
plate{Han
dle r = &/qu*o t;I&NqTuEoRtC;E,P
T I O N
P O I N TS
/ /--Er--r-o-r--/-E-x-c-e-p-t-i-o-n- -H-a-n-d-l--i-n-g--
--- - - -e-xc-ep-t-i-o-n-H-a-n-d-l-e-r- - - - =* &quo/t
;
&q
u o t;,
/ * *
o n I n*v aIl iadmE vceanltl e d a t = t&hqeuo to;n&Rqeuquoets;t,
Ca p t ur ec uistnotmeErrcreoprtTiemonp lpaotien t
= *&qu/ot
;
&q u ovto;i,d
f u n c
t i o n on/Req/uAepsptlCiacpattuiroen( Arsepqeucitrse
d a n y heavnednlte,r Craecqhuiinrge d s t r u c=t ifnatlsee,r
ce p t D aetvae n)t
Ca c hin{g
= vfaarl sre
c = arg}ume;n
ts . e
v e nt./get/CLoalyleocutti Soent(t)i;n
gs
vlaar yopurtcS e=t tairnggusm e=n ts.{ev
en t .g e tdCeoflaluelcttLiaoyno(u tp r= iv&aqtueo=tt;Lrauyeo u);t
. M ain}.cf
m&quo}t;

};

//Register interceptors as an array, we need order
interceptors = [
//Autowire
{class="coldbox.system.interceptors.Autowire"}
];
}

</cfscript>
</cfcomponent>

In my handler all I have to do is call the logger object.

 rc.
wetlhciosm.emMaepspsiangges [= "&q/uointt;eWreclecpotmoer st"o] C=o lCdOBLoDx!B&OqXu_otA;P>P_
RO O
T_ P A T
H & " inter<ceptcfsetors /event."s;e
tV
iew("General/index")
>

</cffunction>

<!------------------------------------------- PRIVATE EVENTS ------------------------------------------>


</cfcomponent>

Sure enough in my console I can see this:

func
ti[olno caadlhdotsotb aCsFk9e.t0(1 ]r:eINqFuO isreidm pelveelnotg g)i
ng . hand{ler
s . G e nvearra lr cE v=e nat rHgaunmdelnetrs .Ceavlelnetd. gEexttCroalIlnecfto:i o
n()
;

sessionstorage.setvar(

Pretty sweet, huh. We can also output some extra information.

 lo
g.info<(&!q-u-o-t ;lEavyeonutt sH/aLnadyloeurt .CMaalilend.c&fqmu o-t-;-, {>ti
me=Now(<), crfc=oructput}>)

>

<cfset< hrtcm.lwel>co
meMessage = "Welcome to< hCeoaldd>Bo
x!">


<ti<tle>cfsetC oelvdebnotx .csaecthViinegw (e&xqaumoptl;eGeneral/index")><
/ t itle><
/cffunction>


<<!-/--h-e-a-d-->--
----------------------------<-b-o-d-y- P>RI
VATE EVENTS -------------<-p--->-L-a-y-o-u-t- -R-e--n-de-re-d-:- -#-N-o-w-(-)-#-->



</<cf/cpom>pon
ent>

<p>Basket Item

The output is:


<!--- views/General/in

There are also different types of messages I can use:

&quo
t;c ohmipnotn=e&nqtu oetx;tMeyn dmsa=i"n ceovlednbto&xq.usoyts;te>m.i
nt
er c e p tor"
<{cfa
rg u mpernotp enratmye =n&aqmueo=t;ev"esnets&sqiuoonts;t orreaqgueir"ed= &iqnujeoctt;=true"&cqoulodtb;ox>:p
lu g i n:Ses<sioncfsetStor avgaer" rc;
= e v
en t .getC/ol*l eIcNtTiEoRnCE(P)TION>
PO I N T S
<--!------- -t-e-l-l- -t-h-e- -L-o-g-B-o-x- i-n-s-t-a-n-c-e- -t-o- -l-o-g- -a-n- -i-n-f-o- -m--e--s-s-a g*e --/-

>

/ **
< *cfset I laomg .cianlfloe(d& qbuyo tt;hEev eornyRtehqiunegs tOCKa&pqtuureo ti;n)ter>c
ep t i o n
< *cfset/
l
og . wvaorind( &fquunoctt;iWoant cohn Roeuqtu!e&sqtuCoatp;t)ur
e>(

re qu i r ed any <evencfsett, r elqougi.reerdr osrt(r&uqcuto ti;nTtoeor cleaptteD&aqtuao t);
)
>

{

var r<c = cfsetargu mleongt.sf.aetvaeln(t&.qgueottC;o*l*lKeAcBtOiOoOnM(*)*;&
qu o t ; )
>

/ /
s i m p le way t<o cocfsetntro lr cc.awcehlicngo mbeaMseesds aogne n=u m&bqeuro to;fW eiltceommse itno tChoel dbBaosxk!e&tq
uo t ; rc
.>b
a s k
et i t e
ms = sessio<nstocfsetrage .egveetnvta.rs(e tVie"wb(a&sqkueotti;tGeemnse"ral, 0/ )i;n
de x &quo}t;)

>

}

</cffunction>

<!------------------------------------------- PRIVATE EVENTS ------------------------------------------>


</cfcomponent>

When I run the event I get the following in my console.


[localhost CF9.01]:INFO simplelogging.handlers.General Everything OK ExtraInfo:
[localhost CF9.01]:WARN simplelogging.handlers.General Watch out! ExtraInfo:
[localhost CF9.01]:ERROR simplelogging.handlers.General Too late ExtraInfo:
[localhost CF9.01]:FATAL simplelogging.handlers.General **KABOOOM** ExtraInfo:

Great now we can categorise logged messages, but logging to console might not suit your needs. Luckily, ColdBox has various different "appenders" which let you choose how you want to display the logged information. Let's configure ColdBox so that the logging information is output to the browser instead by adding a logBox struct to the ColdBox.cfc

/
/L a yout /Se*t*t
in g s
* I lraeydouucteS etthtei nbgasske t= i{te
ms c o undte
fa u l t*L*ayo/u
t
= & qvuooitd; Lfauynocutti.oMna irne.mcofvme&fqruoomtb;a
sk e t( r}equir;e
d e v
e n t )/
/ Reg{ist
er i n tvearrc erpct o=r sa ragsu maen natrsr.aeyv,e nwte. gneeteCdo lolredcetiro
n( ) ;i
nt e r c
ep t o r ss e=s s[
io n s t o rag/e./sAeuttvoawri(r e"
ba s k e t ite{ms"c,l assess=s&iqounosttor;acgoel.dgbeotxv.asry(s t"ebma.skienttietrecmespt"o,r s0. A)u t-o w1i r)e;
&q u o t ;
} s
et N e]x;t E v
e n t
( " g ene/ra/l "c o)n;f
i g ure }Log
Bo
x
logBox = {
appenders = {
coldboxTracer = {
class="coldbox.system.logging.appenders.ColdboxTracerAppender"
}
},
root = { levelmax="DEBUG", levelMin="FATAL", appenders="*" }
};

}

</cfscript>
</cfcomponent>

You'll need to reload the ColdBox framework to pick up the new settings, but when you do you'll see that a new "ColdBox Tracer Messages" section has appeared in the Debugging info in your browser. The super sharp eyed amongest you may have noticed that I set the max logging level to DEBUG. This means that I can make use of the debug status which by default is disabled in ColdBox. To use the debug level simply call:


<cfset log.debug("testing 1,2,3")>

So, we've got some pretty powerful logging going on now, but in production we're not going to want to output to the Console or browser window, instead we want to log to a file so let's add the AsyncRollingFileAppender (Note: Normally, you wouldn't have the ColdboxTracerAppender and the AsyncRollingFileAppender enabled but this is a demo so what the heck!)

    
L}ayo u t R e n de
red: { ts '}2010-1,2-
11 20: 29 :3 2r'o}ot
B=as ket It{ems: 2
vlieew vreenldemread:x =&{qtus o't20;10D-E12B-U1G1& q2u0o:t2;9,: 3l2e'velM}in
=&q
uot;FATAL", appenders="*" }
};

If we reload the config again and check out the logs folder (ColdBox will create it if doesn't exist) and you'll see a file called MYERRORLOG.log which contains:

ew r
en&dqeuroetd;:S eve{rittys& q'u2o0t1;0,-&1q2u-o1t1; A2p0p:e2n8d:e3r8&'quot};,
&q
uot;Date","Time","Category","Message"
"WARN","MYERRORLOG","12/02/2010","14:28:53","simplelogging.handlers.General","Watch out!"
"ERROR","MYERRORLOG","12/02/2010","14:28:53","simplelogging.handlers.General","Too late"
"FATAL","MYERRORLOG","12/02/2010","14:28:53","simplelogging.handlers.General","**KABOOOM**"

So what happened to the DEBUG and INFO messages? Well, when I configured the AsyncRollingFileAppender I set the max logging level to WARN. Anything lower than this is ignored. This is really cool for choosing which types of message you need to log in production.

Logging is a really powerful tool that tends to be overlooked as it's a pain to set up - but you've got no excuses now!


1 comment

  1. Very useful post, John. Thanks!!

    Comment by Tony Garcia – December 02, 2010

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.

Please subscribe me to any further comments
 

Search

Wish List

Found something helpful & want to say ’thanks‘? Then visit my Amazon Wish List :)

Categories

Recent Posts