# Private Log File Helper Function

> Adds a wpide_log() helper that writes timestamped messages and data to a daily log file in a protected folder inside uploads, so custom code can leave a trail without touching debug.log.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: logging, helper, debugging
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/debug-file-logger-helper/

## Code

```php
if ( ! function_exists( 'wpide_log' ) ) {
    function wpide_log( $message, $context = array(), $level = 'info' ) {
        $folder = get_option( 'wpide_log_dir' );
        if ( ! $folder ) {
            $folder = 'wpide-logs-' . wp_generate_password( 12, false );
            update_option( 'wpide_log_dir', $folder, false );
        }

        $uploads = wp_upload_dir( null, false );
        $dir     = trailingslashit( $uploads['basedir'] ) . $folder;

        if ( ! is_dir( $dir ) ) {
            if ( ! wp_mkdir_p( $dir ) ) {
                return false;
            }
            file_put_contents( $dir . '/.htaccess', "Require all denied\nDeny from all\n" );
            file_put_contents( $dir . '/index.php', "<?php\n// Silence is golden.\n" );
        }

        $line = sprintf(
            "[%s] %s: %s%s\n",
            wp_date( 'Y-m-d H:i:s' ),
            strtoupper( sanitize_key( $level ) ),
            is_scalar( $message ) ? (string) $message : wp_json_encode( $message ),
            $context ? ' ' . wp_json_encode( $context, JSON_PARTIAL_OUTPUT_ON_ERROR ) : ''
        );

        return false !== file_put_contents( $dir . '/log-' . wp_date( 'Y-m-d' ) . '.log', $line, FILE_APPEND | LOCK_EX );
    }
}
```

## Notes

Example wpide_log( 'Order synced', array( 'id' => $order_id ), 'info' ); the folder name contains a random suffix and blocks direct web access on Apache.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Private Log File Helper Function" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
