We'll create fresh WordPress site with Bang System Logging installed. You have 20 minutes to test the plugin after that site we'll be deleted.
Enable system logging for WordPress plugin and theme development. This can make it easier to know what your code is doing.
Features include:
Using it is as simple as calling the 'log'
action in your templates or plugin.
<?php do_action('log', 'Some log message'); ?>
This will produce a line in your system log:
Jun 4 11:23:08 myserver php/mysite.com[1553]: b8e mysite.com/path-to-page: Some log message
This includes:
Jun 4 11:23:08
– The date and time of the log messagemyserver
– The name of this computerphp
– The program that produced the messagemysite.com
– The domain name of the site1553
– The process ID of the running PHP processb8e
– A random 3-digit code identifying each page requestmysite.com/path-to-page
– The URL of the requestSome log message
– Your messageFor more detailed instructions, see the How to use tab.
To log a message, use the 'log'
action in your templates or plugins:
<?php do_action('log', 'Some log message'); ?>
This will output the log message:
Some log message
If you include extra parameters, they’ll be added at the end of the log message:
<?php do_action('log', 'A number and a string', 17, 'foo'); ?>
This will produce the log message:
A number and a string: 17, foo
The value will be formatted correctly depending on its type: integers, strings, arrays, objects, booleans etc.
You don’t need to check if values are null or empty, they’ll still be output safely.
If you put the code %s
into your log message, then one of the arguments will be dropped into the message:
<?php do_action('log', 'I have %s numbers', count($numbers), $numbers); ?>
This will produce the log message:
I have 4 numbers: [9, 16, 307, 1]
Logging a complete object – such as a WordPress post – can be very large, and sometimes it’s only one or two fields you need.
If you put a string starting with an exclamation point "!"
followed by a list of field names, they will be selected from the following object.
The following will only show the ID
and post_title
fields of the post:
<?php do_action('log', 'Loaded the post', '!ID,post_title', $post); ?>
This will produce the log message:
Loaded the post: {ID: 1932, post_title: Test page}
If you do this with an array of objects, those fields will be selected from each of them. The following will output a list of post ID
s:
<?php do_action('log', 'Loaded %s posts', count($posts), '!ID', $posts); ?>
This will produce the log message:
Loaded 3 posts: [1932, 1594, 1103]
If you have coloured logging switched on, values will appear in different colours to indicate their type.
This can make for quicker scanning of log files.
To enable coloured logs, tick the Coloured logs checkbox on the settings page.
Then use the log.sh
script, included with this plugin, to decode and display the coloured log files.