Jump to content

Custom Logs


Recommended Posts

Custom Logs

When you use the core $log->save() method you can only save a single string of text. When you view the log in the core ProcessLogger the columns and their header labels are predetermined.

The Custom Logs module is different in that it lets you write and view log files with the number of columns and the column header labels you specify in the module configuration.

Configuration

In the "Custom logs" textarea field, enter custom logs, one per line, in the format...

name: column label, column label, column label

...with as many comma-separated column labels as needed.

The log name must be a word consisting of only [-._a-z0-9] and no extension.

If you prefix a URL column label with {url} then the value in the column will be rendered as a link in the log viewer.

The date/time will automatically be added as the first column so you do not need to specify it here.

Writing to a custom log

Use the CustomLogs::save($name, $data, $options) method to save data to a custom log file.

$cl = $modules->get('CustomLogs');
$cl->save('my-log', $my_data);

Arguments

$name Name of log to save to (word consisting of only [-._a-z0-9] and no extension).

$data An array of strings to save to the log. The number and order of items in the array should match the columns that are configured for the log.

$options (optional) Options for FileLog::save(). Normally you won't need to use this argument.

Example of use

Custom log definition in the module configuration:

visits: {url}URL, IP Address, User Agent, {url}Referrer

Saving data to the log:

$cl = $modules->get('CustomLogs');
$data = [
    $_SERVER['REQUEST_URI'] ?? '',
    $_SERVER['REMOTE_ADDR'] ?? '',
    $_SERVER['HTTP_USER_AGENT'] ?? '',
    $_SERVER['HTTP_REFERER'] ?? '',
];
$cl->save('visits', $data);

Viewing the resulting log in Setup > Logs > visits:

custom-logs-1

 

https://github.com/Toutouwai/CustomLogs
https://processwire.com/modules/custom-logs/

  • Like 9
  • Thanks 2
Link to comment
Share on other sites

Really cool Robin!

Just an FYI for Tracy users - I have pushed a new version which excludes any CustomLogs logs from the PW logs panel because they were just confusing with the incorrect headers and mismatched number of columns. Not sure yet, but I might build a new CustomLogs Panel to properly handle displaying them.

  • Like 3
Link to comment
Share on other sites

6 hours ago, adrian said:

Really cool Robin!

Thanks.

6 hours ago, adrian said:

Just an FYI for Tracy users - I have pushed a new version which excludes any CustomLogs logs from the PW logs panel because they were just confusing with the incorrect headers and mismatched number of columns.

Good call, I didn't think about that panel.

6 hours ago, adrian said:

Not sure yet, but I might build a new CustomLogs Panel to properly handle displaying them.

You don't want to be making changes and additions to Tracy Debugger for the sake of other modules. The appeal of CustomLogs might be quite niche so I'll wait for a bit, but I could perhaps look at including a panel for Tracy within the CustomLogs module. I think I remember reading somewhere that you've allowed for third-party panels - can you refresh my memory on this?

And could there be a way to hook into the ProcessWire Logs panel rendering (if there isn't already)? I have another log-related module to release and I could potentially add a feature so that it applies to the Logs panel too.

  • Like 2
Link to comment
Share on other sites

Hi @Robin S - yes, it is easy to add custom panels to Tracy from your module, eg: https://github.com/wireframe-framework/Wireframe/tree/master/TracyPanels

There isn't a way to hook into the Logs panel though at the moment, however, I think I have reasonable solution based on the new JSON viewing functionality I added recently in the latest version. Here is how that looks when viewing the logs panel with a custom visits log. Obviously not as nice as the PW logs viewer with the way you modified the table, but I think this works well enough and allows easy integration with the PW logs panel and its icon colour change to indicate new entries, combining with the latest from all logs, etc.

Any thoughts before I commit?

image.thumb.png.8e30bb3daf5e8de0c17299d06a06145e.png

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

On 7/10/2024 at 10:25 AM, adrian said:

yes, it is easy to add custom panels to Tracy from your module, eg: https://github.com/wireframe-framework/Wireframe/tree/master/TracyPanels

Thanks, and I just noticed the HelloWorld panel too. Very helpful 😀

On 7/10/2024 at 10:25 AM, adrian said:

I think I have reasonable solution based on the new JSON viewing functionality I added recently in the latest version

That's a good solution, thanks. I missed that there is now a JSON viewing feature. That's what my upcoming module does too. Still worth releasing I think because it has some extra features and config options.

  • Like 3
Link to comment
Share on other sites

@Robin S - I've committed that new version to support CustomLogs in the PW Logs panel.

It was your Logs JSON Viewer module (I found it a couple of weeks ago via Github) that got me thinking about supporting it in the PW and Tracy Logs panels.

  • Like 2
Link to comment
Share on other sites

Quote

The appeal of CustomLogs might be quite niche

And here I was thinking that this is so useful that it should be a core feature 🙂

Anyway, just wanted to say that I absolutely love the concept of this module. In fact both Custom Logs and Logs JSON Viewer are brilliant additions!

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...