Skip to content

Commit 7315e71

Browse files
author
Frederic Dewinne
committed
add console log writer
1 parent c5c699a commit 7315e71

5 files changed

Lines changed: 164 additions & 62 deletions

File tree

module/DeployAgent/config/module.config.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@
170170
'continuous' => __DIR__ . '/../view',
171171
],
172172
],
173+
'log_writers' =>
174+
[
175+
'factories' =>
176+
[
177+
'console' => 'Continuous\DeployAgent\Log\Writer\ConsoleFactory'
178+
]
179+
],
173180
'log' =>
174181
[
175182
'logger/deploy' =>
@@ -183,6 +190,31 @@
183190
'stream' => './data/logs/deploy.log',
184191
],
185192
],
193+
[
194+
'name' => 'console',
195+
'options' =>
196+
[
197+
'filters' =>
198+
[
199+
'priority' =>
200+
[
201+
'name' => 'priority',
202+
'options' =>
203+
[
204+
'priority' => 6
205+
]
206+
],
207+
],
208+
'formatter' =>
209+
[
210+
'name' => 'simple',
211+
'options' =>
212+
[
213+
'format' => '%message%'
214+
]
215+
]
216+
],
217+
],
186218
],
187219
],
188220
]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Console.php
4+
*
5+
* @copyright Copyright (c) 2016 Continuous S.A. (https://continuousphp.com)
6+
* @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
7+
* @file Console.php
8+
* @link http://github.com/continuousphp/deploy-agent the canonical source repo
9+
*/
10+
11+
namespace Continuous\DeployAgent\Log\Writer;
12+
13+
use Zend\Console\ColorInterface;
14+
use Zend\Log\Logger;
15+
use Zend\Log\Writer\AbstractWriter;
16+
use Zend\Console\Adapter\AdapterInterface as ConsoleAdapter;
17+
18+
/**
19+
* Console
20+
*
21+
* @package Continuous\DeployAgent
22+
* @subpackage Log
23+
* @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
24+
*/
25+
class Console extends AbstractWriter
26+
{
27+
/** @var ConsoleAdapter */
28+
protected $console;
29+
30+
/** @var array */
31+
protected $colors = [
32+
Logger::DEBUG => ColorInterface::GRAY,
33+
Logger::INFO => ColorInterface::LIGHT_CYAN,
34+
Logger::NOTICE => ColorInterface::LIGHT_BLUE,
35+
Logger::WARN => ColorInterface::LIGHT_YELLOW,
36+
Logger::ERR => ColorInterface::LIGHT_MAGENTA,
37+
Logger::CRIT => ColorInterface::LIGHT_RED,
38+
Logger::ALERT => ColorInterface::MAGENTA,
39+
Logger::EMERG => ColorInterface::RED
40+
];
41+
42+
/**
43+
* @return ConsoleAdapter
44+
*/
45+
public function getConsole()
46+
{
47+
return $this->console;
48+
}
49+
50+
/**
51+
* @param ConsoleAdapter $console
52+
* @return Console
53+
*/
54+
public function setConsole($console)
55+
{
56+
$this->console = $console;
57+
return $this;
58+
}
59+
60+
protected function doWrite(array $event)
61+
{
62+
if ($this->console) {
63+
$this->console->writeLine($event['message'], $this->getColor($event['priority']));
64+
}
65+
}
66+
67+
protected function getColor($priority)
68+
{
69+
return $this->colors[$priority];
70+
}
71+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* ConsoleFactory.php
4+
*
5+
* @copyright Copyright (c) 2016 Continuous S.A. (https://continuousphp.com)
6+
* @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
7+
* @file ConsoleFactory.php
8+
* @link http://github.com/continuousphp/deploy-agent the canonical source repo
9+
*/
10+
11+
namespace Continuous\DeployAgent\Log\Writer;
12+
13+
use Zend\ServiceManager\FactoryInterface;
14+
use Zend\ServiceManager\ServiceLocatorInterface;
15+
16+
/**
17+
* ConsoleFactory
18+
*
19+
* @package Continuous\DeployAgent
20+
* @subpackage Log
21+
* @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
22+
*/
23+
class ConsoleFactory implements FactoryInterface
24+
{
25+
protected $options = [];
26+
27+
public function __construct(array $options)
28+
{
29+
$this->options = $options;
30+
}
31+
32+
/**
33+
* Create service
34+
*
35+
* @param ServiceLocatorInterface $pluginManager
36+
* @return mixed
37+
*/
38+
public function createService(ServiceLocatorInterface $pluginManager)
39+
{
40+
$writer = new Console($this->options);
41+
42+
$console = $pluginManager->getServiceLocator()->get('Console');
43+
if ($console instanceof \Zend\Console\Adapter\AdapterInterface) {
44+
$writer->setConsole($console);
45+
}
46+
47+
return $writer;
48+
}
49+
}

module/DeployAgent/src/Task/TaskManager.php

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Continuous\DeployAgent\Task\Runner\TaskRunnerManager;
2020
use League\Flysystem\Filesystem;
2121
use Zend\Config\Config;
22-
use Zend\Console\ColorInterface;
2322
use Zend\EventManager\EventInterface;
2423
use Zend\EventManager\EventManagerInterface;
2524
use Zend\EventManager\ListenerAggregateInterface;
@@ -120,24 +119,6 @@ public function setPackageStoragePath($packageStoragePath)
120119
return $this;
121120
}
122121

123-
/**
124-
* @return ConsoleAdapter
125-
*/
126-
public function getConsole()
127-
{
128-
return $this->console;
129-
}
130-
131-
/**
132-
* @param ConsoleAdapter $console
133-
* @return TaskManager
134-
*/
135-
public function setConsole(ConsoleAdapter $console)
136-
{
137-
$this->console = $console;
138-
return $this;
139-
}
140-
141122
/**
142123
* @return mixed
143124
*/
@@ -246,16 +227,12 @@ public function install(EventInterface $event)
246227
$origin = $provider->getSource($build);
247228
$origin->setFilename($build . '.tar.gz');
248229

249-
$this->getLogger()->info('Downloading package...');
250-
if ($this->getConsole()) {
251-
$origin->getEventManager()->attach(
252-
DeployEvent::EVENT_FETCH_PRE,
253-
function (DeployEvent $event) {
254-
$this->getConsole()
255-
->writeLine('Downloading package...', ColorInterface::LIGHT_CYAN);
256-
}
257-
);
258-
}
230+
$origin->getEventManager()->attach(
231+
DeployEvent::EVENT_FETCH_PRE,
232+
function (DeployEvent $event) {
233+
$this->getLogger()->info('Downloading package...');
234+
}
235+
);
259236

260237
$mask = umask(0);
261238

@@ -266,16 +243,12 @@ function (DeployEvent $event) {
266243

267244
$resource = new Archive($resourcePath);
268245

269-
$this->getLogger()->info('Extracting package...');
270-
if ($this->getConsole()) {
271-
$resource->getEventManager()->attach(
272-
DeployEvent::EVENT_FETCH_PRE,
273-
function () {
274-
$this->getConsole()
275-
->writeLine('Extracting package...', ColorInterface::LIGHT_CYAN);
276-
}
277-
);
278-
}
246+
$resource->getEventManager()->attach(
247+
DeployEvent::EVENT_FETCH_PRE,
248+
function () {
249+
$this->getLogger()->info('Extracting package...');
250+
}
251+
);
279252

280253
$destination = new Directory($application->getPath() . DIRECTORY_SEPARATOR . $build);
281254

@@ -316,13 +289,6 @@ public function activate(EventInterface $event)
316289

317290
$message = 'Starting ' . $application->getName() . ' (' . $build . ')';
318291
$this->getLogger()->info($message);
319-
if ($this->getConsole()) {
320-
$this->getConsole()
321-
->writeLine(
322-
'Starting ' . $application->getName() . ' (' . $build . ')',
323-
ColorInterface::LIGHT_CYAN
324-
);
325-
}
326292

327293
symlink(
328294
$application->getPath() . DIRECTORY_SEPARATOR . $build,
@@ -333,19 +299,11 @@ public function activate(EventInterface $event)
333299

334300
$message = $application->getName() . ' (' . $build . ') has successfully started';
335301
$this->getLogger()->info($message);
336-
if ($this->getConsole()) {
337-
$this->getConsole()
338-
->writeLine(
339-
$application->getName() . ' (' . $build . ') has successfully started',
340-
ColorInterface::LIGHT_CYAN
341-
);
342-
}
343302
}
344303

345304
public function loadConfig($path)
346305
{
347306
$this->getLogger()->info('Applying config from ' . $path);
348-
$this->getConsole()->writeLine('Applying config from ' . $path, ColorInterface::WHITE);
349307
$configReader = new \Zend\Config\Reader\Yaml(['Symfony\Component\Yaml\Yaml', 'parse']);
350308
$config = $configReader->fromFile($path);
351309
$this->config = $config;

module/DeployAgent/src/Task/TaskManagerFactory.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ public function createService(ServiceLocatorInterface $serviceLocator)
3333
{
3434
$taskManager = new TaskManager();
3535

36-
if ($serviceLocator->has('Console')) {
37-
/** @var ConsoleAdapter $console */
38-
$console = $serviceLocator->get('Console');
39-
if ($console instanceof ConsoleAdapter) {
40-
$taskManager->setConsole($console);
41-
}
42-
}
43-
4436
/** @var Logger $logger */
4537
$logger = $serviceLocator->get('logger/deploy');
4638
$taskManager->setLogger($logger);

0 commit comments

Comments
 (0)