Make stdout logger use color

This commit is contained in:
Jansen Price 2020-08-26 22:52:57 -05:00
parent 6ae814eebc
commit 213e548593
2 changed files with 35 additions and 13 deletions

View file

@ -32,3 +32,6 @@ openssl req -x509 -newkey rsa:4096 -nodes\
# -keyout "$HOSTNAME.key.pem"\ # -keyout "$HOSTNAME.key.pem"\
# -out "$HOSTNAME.cert.pem" # -out "$HOSTNAME.cert.pem"
# -addext "subjectAltName=DNS:example.com,DNS:www.example.net,IP:10.0.0.1" # -addext "subjectAltName=DNS:example.com,DNS:www.example.net,IP:10.0.0.1"
# To inspect a cert use the following command
#openssl x509 -in <path/to/cert/file> -text -noout

View file

@ -20,6 +20,19 @@ class Console extends \Qi_Console_Client
return 0; return 0;
} }
$config = $this->makeConfig();
if (!$config->quiet) {
print "Orbit // Gemini server software\n";
print ":: Using cert file " . $config->tls_certfile . "\n";
print ":: Using key file " . $config->tls_keyfile . "\n";
}
$server = new Server($config, $this->makeLogger($config));
$server->listen();
}
public function makeConfig()
{
$config = new Config(); $config = new Config();
if ($this->_args->host) { if ($this->_args->host) {
@ -58,21 +71,11 @@ class Console extends \Qi_Console_Client
$config->tls_keyfile = sprintf("%s.key.pem", $config->hostname); $config->tls_keyfile = sprintf("%s.key.pem", $config->hostname);
} }
if (!$config->quiet) { return $config;
print "Orbit // Gemini server software\n";
print ":: Using cert file " . $config->tls_certfile . "\n";
print ":: Using key file " . $config->tls_keyfile . "\n";
}
$server = new Server($config, $this->makeLogger($config));
$server->listen();
} }
public function makeLogger($config) public function makeLogger($config)
{ {
$pid = getmypid();
$output = "[%datetime%] $pid %channel%.%level_name%: %message% %context%\n";
$formatter = new LineFormatter($output, 'Y-m-d\TH:i:s');
$logger = new Logger('orbit'); $logger = new Logger('orbit');
$level = Logger::INFO; $level = Logger::INFO;
@ -81,18 +84,34 @@ class Console extends \Qi_Console_Client
} }
$log_stream = new StreamHandler($config->log_file, $level); $log_stream = new StreamHandler($config->log_file, $level);
$log_stream->setFormatter($formatter); $log_stream->setFormatter($this->makeLogFormatter());
$logger->pushHandler($log_stream); $logger->pushHandler($log_stream);
if (!$config->quiet) { if (!$config->quiet) {
$std_stream = new StreamHandler('php://stdout', $level); $std_stream = new StreamHandler('php://stdout', $level);
$std_stream->setFormatter($formatter); $std_stream->setFormatter($this->makeLogFormatter(true));
$logger->pushHandler($std_stream); $logger->pushHandler($std_stream);
} }
return $logger; return $logger;
} }
public function makeLogFormatter($is_tty = false)
{
$pid = getmypid();
if ($is_tty) {
$output = "{2}[%datetime%]{} {3}$pid %channel%.%level_name%:{} %message% %context%\n";
$output = str_replace("{2}", $this->_terminal->do_setaf(2), $output);
$output = str_replace("{3}", $this->_terminal->do_setaf(3), $output);
$output = str_replace("{}", $this->_terminal->do_op(), $output);
} else {
$output = "[%datetime%] $pid %channel%.%level_name%: %message% %context%\n";
}
return new LineFormatter($output, 'Y-m-d\TH:i:s');
}
public function showVersion() public function showVersion()
{ {
print "Orbit " . Server::$version . "\n"; print "Orbit " . Server::$version . "\n";