Skip to content

Conversation

sapayth
Copy link
Member

@sapayth sapayth commented Jul 14, 2022

A new function is introduced where we can log our messages in a specific file. It will help us debug cron jobs, form settings, payment info etc. Calling a simple function wpuf_log( $message ) will do the trick. It will store a log with the current date time in the wp-content/uploads/wpuf-logs/ directory. A new log file will be created for each day and all the log files older than 60 days will be removed.

How to use

Simply calling wpuf_log( $message ) will store the log message with datetime. This function also have 2 others optional parameter to pass. So the three parameters are:

$message [string]
The message to log

$level [string]
The log level. Default info

Description of levels:
'emergency': System is unusable.
'alert': Action must be taken immediately.
'critical': Critical conditions.
'error': Error conditions.
'warning': Warning conditions.
'notice': Normal but significant condition.
'info': Informational messages.
'debug': Debug-level messages.

$log_file [string|resource]
Optional parameter to pass the log file. The log will be written in this file instead of the default log file.

A basic use:

wpuf_log( sprintf(
	'Post form: %d is set to expire after %s %s. Expired post status: %s',
	$data['form_id'],
	$time,
	$type,
	$status
));

Will store a log message like:

[12/Jul/2022:08:07:13 +0000] INFO: Post form: 973 is set to expire after 2 day. Expired post status: draft

Hooks Introduced

wpuf_log_directory [filter]: The default log directory is wp-content/uploads/wpuf-logs/. We can change it using this filter.
wpuf_log_file_name [filter]: We can change the log file name entirely using this filter.
wpuf_formatted_log_message [filter]: The default log format is [%datetime%] %level%: %message%\n. With this filter we will get the formatted log message.
wpuf_logger_expiration_days [filter]: Log file will be deleted after 60 days. We can change it using this filter.

@sapayth sapayth added the needs: dev review This PR needs review by a developer label Jul 14, 2022
sapayth added 3 commits July 14, 2022 09:47
A blank index.php file in the directory will prevent accessing the directory directly
1. Made WPUF_File_Logger object singleton
2. Logging disabled for debug mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: dev review This PR needs review by a developer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants