Ready-made extensions and custom development for Joomla!

  • Home
  • Blog
  • Joomla dev best practices: more on the JLog class

Joomla dev best practices: more on the JLog class

We already had a very quick intro to the JLog class in our best practices article about basic error management.

But since then our usage of that method has grown a lot, so now we're able to tell you a bit more.

Start simple

Let's start with the simplest way to log.

First of all define the file that you want to write to.

You could also write to the main Joomla log file, but we reccomend to always use a separate log file for your component. Even more, we often use several different log files for different parts of the same component.

So add the new logger file:

JLog::addLogger( 
array(
'text_file' => 'com_books.log.php'
)
);

Next, you can start logging with:

JLog::add(
"Creating a log record"
);

Done!

But if you stop here, your log is going to contain too many records...

Add priority

A good idea is to have different log files for different priorities. 

The available priorities are:

  • JLog::EMERGENCY
  • JLog::ALERT
  • JLog::CRITICAL
  • JLog::ERROR
  • JLog::WARNING
  • JLog::NOTICE
  • JLog::INFO
  • JLog::DEBUG

Of course you can also use JLog::ALL to just catch everything, which is the default.

When you add a logger, you can set the priority records that it should handle:

JLog::addLogger( 
array(
'text_file' => 'com_books.warning.log.php'
),
JLog::WARNING
);
JLog::addLogger( 
array(
'text_file' => 'com_books.info.log.php'
),
JLog::INFO
);

Now whenever you log something you can add a priority so it will end up in the right file:

JLog::add(
"Creating an INFO log record", JLog::INFO
);
JLog::add(
"Creating a WARNING log record", JLog::WARNING
);

It is not mandatory to send different priorities to different files; you can still use priorities when adding records, even if they all end up in the same file.

The priority will be added to the row, so you can quickly recognize the most important log records.

Add category

At this point, everything is almost setup. But you will get log messages from other extensions as well!

So what you need to do is add a category.

JLog::addLogger( 
array(
'text_file' => 'com_books.info.log.php'
),
JLog::ALL,
'com_books'
);
JLog::add(
"Creating an INFO log record", JLog::INFO, 'com_books'
);

The category will also be written into the log, after the priority.

Keep your logs small

Logging can be extremely useful when trying to find a cause for a malfunction. Instead of having to re-run the process and make the error appear again, you can simply read the logs and find out what happened.

Once you discover this, you will be tempted to log everything! :-)

And that's a good idea.

But keep in mind that your log could grow a lot, and become unreadable. Ever tried opening a text file of some hundred MB?

A simple trick to get a different log file for each day is to build the filename so that it includes the date:

JLog::addLogger( 
array(
'text_file' => 'com_books.' . date('Y-m-d') . 'info.log.php'
),
JLog::ALL,
'com_books'
);

Conclusions

Log is an excellent way to keep track of what's happening without bothering users with debug error messages. 

So our suggestions is to start use it immediately in each of your extensions.

(thanks to this article from Andrew Eddie where we picked up some additional details)