Apologies for the noob question: I’ve been watchin...
# box-products
p
Apologies for the noob question: I’ve been watching the LogBox 101 videos and reading the docs. A quick question - instantiating the LogBox system in the
application
scope makes sense, but in the demos (and in the docs) the pages create a new
log
object from
application.LogBox
and then run its methods. Do I have to create a new
log
object on each page or can there be something else in a broader scope? I’d like to just be able to call the
log.error();
commands where needed. Either I’m dumb and should know that I have to create the
log
object or there’s another place to put it. Any pointers would be appreciated.
b
When you say "create a new log", can you show the code that you're referring to?
@Peter Hoopes
If you just mean
Copy code
logbox.getLogger( 'foo' )
then that's not "creating a new logger", it's just giving you the logger you asked for.
They are created once, they first time you ask for them.
If you are inside of a CFC, I'd recommend doing
Copy code
variables.log = application.logbox.getLogger( this );
in the
init()
for that CFC and from then on out, you can just run
Copy code
<http://log.info|log.info>()
when you want
But if you want to use LogBox in a .cfm file, you just need to get it every time
p
I guess I’m thinking about the demo files in the CFCasts that use a simple CFM page to demo the logs. At the top, the page code
log = application.logbox.getRootLogger();
is what I’m wondering about. Much of my app is still CFM based (being rewritten), so I guess I’ll need to call that on each page. I think I understand now…. thanks.
b
Yeah, that's not recreating anything
Logbox internally caches all the logger objects so once it's created, you're just getting a reference to it
p
Got it. I suppose no issue with me putting it in the onRequestStart handler?
b
No
Though, I feel like I should add it's a little heavy handed to just use the root logger for EVERYTHING.
Sure, it's possible and maybe fine if your logging needs are very rudimentary but Logbox was designed to allow you to specify where the logging messages were coming from
Which then allows you to create custom appenders, max/min filters, etc based on different parts of your site
When you call something like
Copy code
application.logbox.getLogger( this );
then the name of the logger is
<http://path.to|path.to>.your.cfc
instead of just
root
Now, all the log messages in the logs have the category in them which tells you where the message came from
If you're not in a CFC, you could at least do something like
Copy code
application.logbox.getLogger( 'shoppingcart' );
 application.logbox.getLogger( 'login' );
 application.logbox.getLogger( 'dashboard' );
and that will be the category name
p
Got it. That makes sense. Thanks!