Basic Logging with PHP and Syntax Highlighting in Jekyll

February 23, 2016
Category: TIL
Tags: PHP and Jekyll

Today I learned:

Basic Logging to CSV in PHP

I asked Eric Davis about logging for debugging and usage stats on my Toggl Slack slash command and he suggested that I look into writing to a CSV or JSON file. I opted for CSV.

Here I build the array from the information I get back from Slack (plus a datetime stamp) and pass it to a file called log.csv via fputcsv().

$log_array = array($date,$user_id,$user_name,$command,$text,$response_url,$channel_id,$channel_name,$token,$team_id,$team_domain);
$output = fopen('log.csv', 'a');
fputcsv($output, $log_array);

A note on fopen() - A lot of tutorials replace that 'a' with a 'w'. According to the docs, w writes at the beginning of the file, which is why my first couple tries overwrote each other. The a starts writing at the end of the file, which is always a new line since fputcsv() always ends with a line break.

Syntax Highlighting

I finally found a good solution for syntax highlighting in code blocks with Jekyll: Kramdown with Pygments plugin.

Installation is simple: Clone the project to your _plugins folder and add it to the Plugins section of your _config.yaml, then you are ready to go.

Usage is equally as simple: Specify the language at the end of the first line of your code blocks and the plugin will add the proper classes and highlight the code according to the language.


~~~~ php
echo "Hello world!";

Will output:

echo "Hello world!";

Find this post useful?

Buy me a coffeeBuy me a coffee