PHP Classes

File: src/config/logger.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   CMS Airship   src/config/logger.php   Download  
File: src/config/logger.php
Role: Example script
Content type: text/plain
Description: Example script
Class: CMS Airship
Content management system with security features
Author: By
Last change: Universal mbstring.func_overload resistance
Use http_response_code() instead of a manual string.

See #161
Date: 7 years ago
Size: 2,011 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);

use
Airship\Engine\LedgerStorage\{
   
DBStore,
   
FileStore
};
use
Airship\Engine\{
   
Gears,
   
State
};
use
ParagonIE\ConstantTime\Binary;

/**
 * Configure the application event logger here
 */
$log_setup_closure = function() {
   
   
$state = State::instance();
   
$loggerClass = Gears::getName('Ledger');
   
$args = [];
   
   
/**
     * Here we build our logger storage class
     */
   
switch ($state->universal['ledger']['driver']) {
        case
'file':
           
$path = $state->universal['ledger']['path'];
            if (
Binary::safeStrlen($path) >= 2) {
                if (
$path[0] === '~' && $path[1] === '/') {
                   
$path = ROOT . '/' . Binary::safeSubstr($path, 2);
                }
            }
           
$storage = new FileStore(
               
$path,
               
$state->universal['ledger']['file-format'] ?? FileStore::FILE_FORMAT,
               
$state->universal['ledger']['time-format'] ?? FileStore::TIME_FORMAT
           
);
            break;
        case
'database':
           
$path = $state->universal['ledger']['connection'];
            try {
               
$storage = new DBStore(
                   
$path,
                   
$state->universal['ledger']['table'] ?? DBStore::DEFAULT_TABLE
               
);
            } catch (\
Throwable $ex) {
                \
http_response_code(500);
                echo \
file_get_contents(
                   
__DIR__ . '/error_pages/uncaught-exception.html'
               
);
                if (
$state->universal['debug']) {
                    echo
'<pre>', $ex->getTraceAsString(), '</pre>';
                }
                exit(
1);
            }
            break;
        default:
            throw new \
Exception('Invalid logger storage mechanism');
    }

   
/**
     * We inject any more dependencies here:
     */
   
$logger = new $loggerClass($storage, ...$args);
   
$state->logger = $logger;
};
$log_setup_closure();
unset(
$log_setup_closure);