<?php

require_once $_SERVER["DOCUMENT_ROOT"] . '/vendor/autoload.php';

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

date_default_timezone_set("Asia/Shanghai");

function getStream($path)
{
    // the default date format is "Y-m-d\TH:i:sP"
    $dateFormat = "Y-n-j H:i:s";
// the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
    $output = "%datetime% > %level_name% > %message% %context% %extra%\n";
// finally, create a formatter
    $formatter = new LineFormatter($output, $dateFormat);
// create a log channel

    try {
        $stream = new StreamHandler($path, Logger::INFO);
        $stream->setFormatter($formatter);
    } catch (Exception $e) {
        error_log("init Monolog Stream config error");
    }
    return $stream;
}

function getLogger()
{
    $log = new Logger('name');
    $log->pushHandler(getStream($_SERVER["DOCUMENT_ROOT"] . "/log/run.log"));
    $log->pushHandler(getStream("php://stdout")); // <<< uses a stream
    return $log;
}

function info($msg)
{
    getLogger()->info($msg);
}

function error($msg)
{
    getLogger()->error($msg);
}