Overview

Packages

  • tipy

Classes

  • Tipy
  • TipyApp
  • TipyCli
  • TipyCliSession
  • TipyConfig
  • TipyController
  • TipyCookie
  • TipyDAO
  • TipyEnv
  • TipyFlash
  • TipyInflector
  • TipyInput
  • TipyIOWrapper
  • TipyLogger
  • TipyMailer
  • TipyModel
  • TipyOutput
  • TipyRequest
  • TipySession
  • TipyTestCase
  • TipyTestRunner
  • TipyView

Exceptions

  • AssertionFailedException
  • CompileErrorException
  • CompileWarningException
  • CoreErrorException
  • CoreWarningException
  • DeprecatedException
  • NoMethodException
  • NoticeException
  • ParseException
  • RecoverableErrorException
  • StrictException
  • TipyDaoException
  • TipyException
  • TipyModelException
  • TipyRollbackException
  • TipyValidationException
  • UserDeprecatedException
  • UserErrorException
  • UserNoticeException
  • UserWarningException
  • WarningException
  • Overview
  • Package
  • Class
  • Deprecated
  • Todo
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 
<?php
/**
 * TipyLogger
 *
 * @package tipy
 */

/**
 * Simple Logger
 *
 * By default TipyLogger writes to *STDERR*
 * <code>
 * // This will output to apache's error.log  something like
 * // Fri Sep 13 12:22:20 2015 [23685] [INFO] Hello World! 
 * $logger = new TipyLogger();
 * $logger->info('Hello World');
 * </code>
 *
 * But it is possible to create a logger to write to the file
 * <code>
 * $logger = new TipyLogger('/var/log/project.log');
 * $logger->info('Hello World');
 * </code>
 *
 * ## Threshold Level
 *
 * Default threshold level for new TipyLogger is {@link DEBUG}.
 * This means that all messages are logged. 
 *
 * You can change {@link $threshold} to different threshold level 
 * and then only messages with lower or equal severity level will
 * be logged.
 * <code>
 * $logger = new TipyLogger('/var/log/project.log');
 * $logger->threshold = TipyLogger::WARN;
 * $logger->warn('I am logged :)'); // logged
 * $logger->info('I am not :('); // ignored
 * </code>

 */
class TipyLogger {

    // Severity Levels

    /**
     * An unhandleable error that results in a program crash
     */
    const FATAL = 1;

    /**
     * A handleable error
     */
    const ERROR = 2;

    /**
     * A warning
     */
    const WARN = 3;

    /**
     * Useful information
     */
    const INFO = 4;

    /**
     * Low-level information for developers
     */
    const DEBUG = 5;

    /**
     * Logging is turned off
     */
    const OFF = 0;

    /**
     * Level names array for quick *level=>name* conversion
     * @var array
     */
    private $levelNames = ['OFF', 'FATAL', 'ERROR', 'WARN', 'INFO', 'DEBUG'];

    /**
     * Log level threshold
     * @var integer
     */
    public $threshold = self::DEBUG;

    /**
     * Log format. Accepts all strftime characters plus *%pid* and *%level*
     * @var string
     */
    public $format = '%c [%pid] [%level]';

    /**
     * Path to the log file
     * @var string
     */
    private $filePath;

    /**
     * Log resource handler
     * @var resource
     */
    private $handle;

    /**
     * Create new TipyLogger instance
     *
     * @param string $filePath default {@link 'php://stderr'}
     */
    public function __construct($filePath = 'php://stderr') {
        $this->filePath = $filePath;
        $this->handle = fopen($this->filePath, 'a');
    }

    /**
     * Close open log file
     * @internal
     */
    public function __destruct() {
        fclose($this->handle);
    }

    /**
     * Set threshold level
     * @param string|integer $level
     */
    public function setThreshold($level) {
        if (is_int($level)) {
            if ($level < TipyLogger::OFF or $level > TipyLogger::DEBUG) {
                throw new TipyException("Unknown log level ".$level);
            }
            $this->threshold = $level;
        } else {
            $level = strtoupper($level);
            if (!in_array($level, $this->levelNames)) {
                throw new TipyException("Uknown log level ".$level);
            }
            $this->threshold = array_flip($this->levelNames)[$level];
        }
    }

    /**
     * Add log prefix to mesage
     * @param string $message
     * @param integer $level
     * @return string
     */
    private function formatMessage($message, $level) {
        $prefix = str_replace('%pid', posix_getpid(), $this->format);
        $prefix = str_replace('%level', $this->levelNames[$level], $prefix);
        $prefix = strftime($prefix);
        return $prefix.' '.$message.PHP_EOL;
    }

    /**
     * Log message
     * @param string $message
     * @param integer $level
     */
    public function log($message, $level) {
        if ($level <= $this->threshold) {
            fwrite($this->handle, $this->formatMessage($message, $level));
        }
    }

    /**
     * Log FATAL message
     * @param string $message
     */
    public function fatal($message) {
        $this->log($message, self::FATAL);
    }

    /**
     * Log ERROR message
     * @param string $message
     */
    public function error($message) {
        $this->log($message, self::ERROR);
    }

    /**
     * Log WARN message
     * @param string $message
     */
    public function warn($message) {
        $this->log($message, self::WARN);
    }

    /**
     * Log INFO message
     * @param string $message
     */
    public function info($message) {
        $this->log($message, self::INFO);
    }

    /**
     * Log DEBUG message
     * @param string $message
     */
    public function debug($message) {
        $this->log($message, self::DEBUG);
    }

}
tipy API documentation generated by ApiGen