Robin S Posted July 10 Share Posted July 10 Logs JSON Viewer Formats JSON data in ProcessLogger for improved readability. Because log files can only contain strings, it's a common practice to use json_encode() to convert an array to a string when you want to save the data to a log file. But the resulting JSON is not as readable as it could be when viewing the log in ProcessLogger. The Logs JSON Viewer module uses the json-viewer library to improve the readability of JSON data in ProcessLogger and add some useful features. Before: After: Configuration You can set the config options for json-viewer in a textarea field. See the json-viewer readme for information about the options. There is also an option to set the width of the column that contains the JSON data. This setting exists because otherwise the column jumps around in an inconsistent and distracting way when changing from log to log or between paginations. Features You can switch the view of the JSON data between formatted and unformatted using the toggle button at the bottom of the data. The viewer has a number of useful features such as: Progressively expand or collapse levels in the data. View the count of child items and the data type of each item. Search for a string within the data. Copy all or part of the data to the clipboard (requires the HTTPS protocol). https://github.com/Toutouwai/LogsJsonViewer https://processwire.com/modules/logs-json-viewer/ 11 3 Link to comment Share on other sites More sharing options...
wbmnfktr Posted July 10 Share Posted July 10 You have a run here. It's impressive! ? 4 Link to comment Share on other sites More sharing options...
szabesz Posted July 10 Share Posted July 10 9 hours ago, Robin S said: Because log files can only contain strings, it's a common practice to use json_encode() to convert an array to a string when you want to save the data to a log file. Sure, I also do that ? Thanks a 1000000 for sharing one of your modules again! 1 Link to comment Share on other sites More sharing options...
FireWire Posted July 27 Share Posted July 27 I love this module. 1 Link to comment Share on other sites More sharing options...
MarkE Posted August 29 Share Posted August 29 Nice module @Robin S. I have just one problem - if the string is too long, it gets truncated in the log and then doesn't conform to the json format so the module doesn't transform it. Any ideas? Link to comment Share on other sites More sharing options...
Robin S Posted August 29 Author Share Posted August 29 2 hours ago, MarkE said: if the string is too long, it gets truncated in the log LogsJsonViewer doesn't do any truncating, and doesn't get involved in the saving of log data at all - it just formats what is already in the log. Your problem might be due to $maxLineLength in FileLog. There's a FileLog::setMaxLineLength() method, but I'm not sure how you could practically use this unless you write to your log file using FileLog::save() instead of the more convenient WireLog::save(). Instead you probably just have to try and avoid going over $maxLineLength if you are saving JSON, perhaps by doing your own truncation on individual values if they are long, before you save the data. Link to comment Share on other sites More sharing options...
szabesz Posted September 27 Share Posted September 27 @Robin S Hello, Thanks for sharing the module! This is the first time I am using it, and I find that contrary to this, quote: "$cl->save('my-log', $my_data);" If I use hyphen in the specified filename then this kicks in: // Return early if this log is not configured as a custom log As soon as I use underscores instead, all is good. Link to comment Share on other sites More sharing options...
Robin S Posted September 27 Author Share Posted September 27 @szabesz, this is the support thread for Logs JSON Viewer but I think you are talking about the Custom Logs module. Custom Logs is working for me here with a log named "my-log", so it would be good to find out why it's not working for you. Do you have a custom log named "my-log" defined in the module config? What do you see if you use Tracy to do this... bd($name, "name"); bd($custom_logs, "custom_logs"); ...just before this line. 1 Link to comment Share on other sites More sharing options...
szabesz Posted September 30 Share Posted September 30 @Robin S I am sorry for the confusion, the issue is related to the Custom Logs module for sure. I mixed up the modules because both support threads were open in my browser, and I picked the wrong one. I use the modules in tandem, as that is the most obvious way to have the best log viewing experience. As for the issue: "Do you have a custom log named 'my-log' defined in the module config?" Yes, I did set the file's name (without the extension) in the module config. The good news is that it has now "started to work". I'm not sure what I did right this time, but renaming it in the config and in my code fixed the issue. Maybe some invisible character was involved, I don't know (that's just my best guess), as I had definitely tried to make it work before I reported my issue. Thanks for both modules and your support as well! 1 Link to comment Share on other sites More sharing options...
FireWire Posted Wednesday at 10:56 PM Share Posted Wednesday at 10:56 PM If you're using this module, this little hook can put a cherry on top 🤌 <?php namespace ProcessWire; use stdClass; /** * Automatically converts array or object log values to JSON */ wire()->addHookBefore("WireLog::save",function (HookEvent $e) { $text = $e->arguments('text'); (is_array($text) || $text instanceof stdClass) && $e->arguments('text', json_encode($text)); }); // Nice $log->save('nifty-log', [ 'this' => 'works', 'no' => 'problem', 'so' => [ 'does', 'this' ] ]); 3 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now