Skip to content

Instantly share code, notes, and snippets.

@inetfuture
Created August 1, 2016 13:41
Show Gist options
  • Select an option

  • Save inetfuture/61a40d1cf5ec9b623afe7ab95b201191 to your computer and use it in GitHub Desktop.

Select an option

Save inetfuture/61a40d1cf5ec9b623afe7ab95b201191 to your computer and use it in GitHub Desktop.

Revisions

  1. inetfuture created this gist Aug 1, 2016.
    14 changes: 14 additions & 0 deletions Employee.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    class Employee {
    public Money calculatePay() {
    switch (this.type) {
    case COMMISSIONED:
    return this.calculateCommissionedPay();
    case HOURLY:
    return this.calculateHourlyPay();
    case SALARIED:
    return this.calculateSalariedPay();
    default:
    throw new InvalidEmployeeType(this.type);
    }
    }
    }
    60 changes: 60 additions & 0 deletions Logger.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    <?php

    class Logger {
    public static function log($message, $context, $isDebug) {
    $logFile = Yii::$app->getRuntimePath() . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . 'test.log';
    $logPath = dirname($logFile);
    if (!is_dir($logPath)) {
    FileHelper::createDirectory($logPath, 0775, true);
    }

    if (is_string($message)) {
    if (is_array($context)) {
    $time = new DateTime()->format('Y-m-d H:i:s');
    $level = $isDebug ? 'debug' : 'info';
    $logText = "[$time] [$level] [$message] $context";

    $file = @fopen($logFile, 'a');
    clearstatcache();
    if (filesize($logFile) > 1024 * 1024) {
    $logPath = dirname($logFile);
    $filenames = scandir($logPath);
    $maxIndex = 0;
    foreach ($filenames as $filename) {
    if (is_file($logPath . DIRECTORY_SEPARATOR . $filename)) {
    if (strpos($filename, self::FILE_NAME . '.') !== false) {
    $newIndex = substr($filename, strlen(self::FILE_NAME . '.'));
    $maxIndex = max($maxIndex, $newIndex);
    }
    }
    }

    while ($maxIndex >= 0) {
    $rotateFile = $logFile . ($maxIndex == 0 ? '' : '.' . $maxIndex);
    @copy($rotateFile, $logFile . '.' . ($maxIndex + 1));
    if ($file = @fopen($rotateFile, 'a')) {
    @ftruncate($file, 0);
    @fclose($file);
    }

    $maxIndex--;
    }

    fclose($file);
    file_put_contents($logFile, $text, FILE_APPEND);
    } else {
    fwrite($file, $text);
    fclose($file);
    }

    chmod($logFile, 0775);

    return true;
    } else {
    return false;
    }
    } else {
    return false;
    }
    }
    }