Skip to content

Commit d300e0d

Browse files
author
Takashi Matsuo
authored
Adding ListEntries and DeleteLogger commands (GoogleCloudPlatform#155)
* Adding ListEntries and DeleteLogger commands
1 parent 0fdf5a3 commit d300e0d

File tree

8 files changed

+319
-23
lines changed

8 files changed

+319
-23
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818

1919
require __DIR__ . '/vendor/autoload.php';
2020

21-
use Google\Cloud\Samples\Logging\Write;
21+
use Google\Cloud\Samples\Logging\DeleteLoggerCommand;
22+
use Google\Cloud\Samples\Logging\ListEntriesCommand;
23+
use Google\Cloud\Samples\Logging\WriteCommand;
2224
use Symfony\Component\Console\Application;
2325

2426
$application = new Application();
25-
$application->add(new Write());
27+
$application->add(new DeleteLoggerCommand());
28+
$application->add(new ListEntriesCommand());
29+
$application->add(new WriteCommand());
2630
$application->run();
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
/**
2525
* A base class for commands which needs project id.
2626
*/
27-
class CommandWithProject extends Command
27+
abstract class BaseCommand extends Command
2828
{
2929
/**
30-
* Add project option to $this.
30+
* Add --project and --logger options.
3131
*/
32-
protected function addProjectOption()
32+
protected function addCommonOptions()
3333
{
3434
$this->addOption(
3535
'project',
@@ -39,6 +39,15 @@ protected function addProjectOption()
3939
'If omitted then the current gcloud project is assumed. ',
4040
$this->getProjectIdFromGcloud()
4141
);
42+
$this->addOption(
43+
'logger',
44+
null,
45+
InputOption::VALUE_OPTIONAL,
46+
'The name of the logger. By naming a logger, you can logically '
47+
. 'treat log entries in a logger; e.g. you can list or delete '
48+
. 'all the log entries by the name of the logger.',
49+
'my_logger'
50+
);
4251
}
4352

4453
protected function interact(InputInterface $input)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Logging;
19+
20+
// [START delete_logger_use]
21+
use Google\Cloud\Logging\LoggingClient;
22+
// [END delete_logger_use]
23+
use Symfony\Component\Console\Input\InputInterface;
24+
use Symfony\Component\Console\Output\OutputInterface;
25+
26+
/**
27+
* Class DeleteLoggerCommand
28+
* @package Google\Cloud\Samples\Logging
29+
*
30+
* This command deletes a logger and all its entries.
31+
*/
32+
class DeleteLoggerCommand extends BaseCommand
33+
{
34+
protected function configure()
35+
{
36+
$this
37+
->setName('delete-logger')
38+
->setDescription('Deletes the given logger and its entries');
39+
$this->addCommonOptions();
40+
}
41+
42+
protected function execute(InputInterface $input, OutputInterface $output)
43+
{
44+
$projectId = $input->getOption('project');
45+
$loggerName = $input->getOption('logger');
46+
// [START delete_logger]
47+
$logging = new LoggingClient(['projectId' => $projectId]);
48+
$logger = $logging->logger($loggerName);
49+
$logger->delete();
50+
// [END delete_logger]
51+
printf("Deleted a logger '%s'.\n", $loggerName);
52+
}
53+
}

logging/src/ListEntriesCommand.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Logging;
19+
20+
// [START list_log_use]
21+
use Google\Cloud\Logging\LoggingClient;
22+
// [END list_log_use]
23+
use Symfony\Component\Console\Input\InputInterface;
24+
use Symfony\Component\Console\Output\OutputInterface;
25+
26+
/**
27+
* Class ListEntriesCommand
28+
* @package Google\Cloud\Samples\Logging
29+
*
30+
* This command simply lists log messages.
31+
*/
32+
class ListEntriesCommand extends BaseCommand
33+
{
34+
protected function configure()
35+
{
36+
$this
37+
->setName('list-entries')
38+
->setDescription('Lists log entries in the logger');
39+
$this->addCommonOptions();
40+
}
41+
42+
protected function execute(InputInterface $input, OutputInterface $output)
43+
{
44+
$project = $input->getOption('project');
45+
$loggerName = $input->getOption('logger');
46+
// [START list_log]
47+
$logging = new LoggingClient(['projectId' => $project]);
48+
$loggerFullName = sprintf('projects/%s/logs/%s', $project, $loggerName);
49+
$options = [
50+
'filter' => sprintf('logName = "%s"', $loggerFullName)
51+
];
52+
foreach ($logging->entries($options) as $entry) {
53+
/* @var $entry \Google\Cloud\Logging\Entry */
54+
printf(
55+
"%s : %s\n",
56+
$entry->info()['timestamp'],
57+
$entry->info()['textPayload']
58+
);
59+
}
60+
// [END list_log]
61+
}
62+
}
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
// [START write_log_use]
2121
use Google\Cloud\Logging\LoggingClient;
2222
// [END write_log_use]
23-
use Symfony\Component\Console\Input\InputInterface;
2423
use Symfony\Component\Console\Input\InputArgument;
24+
use Symfony\Component\Console\Input\InputInterface;
2525
use Symfony\Component\Console\Output\OutputInterface;
2626

2727
/**
28-
* Class Write
28+
* Class WriteCommand
2929
* @package Google\Cloud\Samples\Logging
3030
*
3131
* This command simply writes a log message via Logging API.
3232
*/
33-
class Write extends CommandWithProject
33+
class WriteCommand extends BaseCommand
3434
{
3535
protected function configure()
3636
{
@@ -43,16 +43,17 @@ protected function configure()
4343
"The log message to write",
4444
"Hello"
4545
);
46-
$this->addProjectOption();
46+
$this->addCommonOptions();
4747
}
4848

4949
protected function execute(InputInterface $input, OutputInterface $output)
5050
{
51-
$message = $input->getArgument("message");
52-
$project = $input->getOption('project');
51+
$message = $input->getArgument('message');
52+
$projectId = $input->getOption('project');
53+
$loggerName = $input->getOption('logger');
5354
// [START write_log]
54-
$logging = new LoggingClient(['projectId' => $project]);
55-
$logger = $logging->logger('my_logger');
55+
$logging = new LoggingClient(['projectId' => $projectId]);
56+
$logger = $logging->logger($loggerName);
5657
$entry = $logger->entry($message, [
5758
'type' => 'gcs_bucket',
5859
'labels' => [
@@ -61,6 +62,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
6162
]);
6263
$logger->write($entry);
6364
// [END write_log]
64-
print "Wrote a log to a logger.\n";
65+
printf("Wrote a log to a logger '%s'.\n", $loggerName);
6566
}
6667
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Logging\Tests;
19+
20+
use Google\Cloud\Samples\Logging\DeleteLoggerCommand;
21+
use Google\Cloud\Samples\Logging\WriteCommand;
22+
use Symfony\Component\Console\Application;
23+
use Symfony\Component\Console\Tester\CommandTester;
24+
25+
/**
26+
* Functional tests for DeleteLoggerCommand.
27+
*/
28+
class DeleteLoggerCommandTest extends \PHPUnit_Framework_TestCase
29+
{
30+
/* @var $hasCredentials boolean */
31+
protected static $hasCredentials;
32+
/* @var $projectId mixed|string */
33+
private $projectId;
34+
35+
public static function setUpBeforeClass()
36+
{
37+
$path = getenv('GOOGLE_APPLICATION_CREDENTIALS');
38+
self::$hasCredentials = $path && file_exists($path) &&
39+
filesize($path) > 0;
40+
}
41+
42+
public function setUp()
43+
{
44+
if (!self::$hasCredentials) {
45+
$this->markTestSkipped('No application credentials were found.');
46+
}
47+
48+
if (!$this->projectId = getenv('GOOGLE_PROJECT_ID')) {
49+
$this->markTestSkipped('No project ID');
50+
}
51+
$application = new Application();
52+
$application->add(new WriteCommand());
53+
$commandTester = new CommandTester($application->get('write'));
54+
$commandTester->execute(
55+
[
56+
'--project' => $this->projectId,
57+
'--logger' => 'my_test_logger',
58+
'message' => 'Test Message'
59+
],
60+
['interactive' => false]
61+
);
62+
sleep(2);
63+
}
64+
65+
public function testDeleteLogger()
66+
{
67+
$application = new Application();
68+
$application->add(new DeleteLoggerCommand());
69+
$commandTester = new CommandTester($application->get('delete-logger'));
70+
$commandTester->execute(
71+
['--project' => $this->projectId, '--logger' => 'my_test_logger'],
72+
['interactive' => false]
73+
);
74+
$this->expectOutputRegex(
75+
sprintf("/Deleted a logger '%s'./", 'my_test_logger')
76+
);
77+
}
78+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Logging\Tests;
19+
20+
use Google\Cloud\Samples\Logging\ListEntriesCommand;
21+
use Google\Cloud\Samples\Logging\WriteCommand;
22+
use Symfony\Component\Console\Application;
23+
use Symfony\Component\Console\Tester\CommandTester;
24+
25+
/**
26+
* Functional tests for ListEntriesCommand.
27+
*/
28+
class ListEntriesCommandTest extends \PHPUnit_Framework_TestCase
29+
{
30+
/* @var $hasCredentials boolean */
31+
protected static $hasCredentials;
32+
/* @var $projectId mixed|string */
33+
private $projectId;
34+
35+
public static function setUpBeforeClass()
36+
{
37+
$path = getenv('GOOGLE_APPLICATION_CREDENTIALS');
38+
self::$hasCredentials = $path && file_exists($path) &&
39+
filesize($path) > 0;
40+
}
41+
42+
public function setUp()
43+
{
44+
if (!self::$hasCredentials) {
45+
$this->markTestSkipped('No application credentials were found.');
46+
}
47+
48+
if (!$this->projectId = getenv('GOOGLE_PROJECT_ID')) {
49+
$this->markTestSkipped('No project ID');
50+
}
51+
$application = new Application();
52+
$application->add(new WriteCommand());
53+
$commandTester = new CommandTester($application->get('write'));
54+
$commandTester->execute(
55+
[
56+
'--project' => $this->projectId,
57+
'--logger' => 'my_test_logger',
58+
'message' => 'Test Message'
59+
],
60+
['interactive' => false]
61+
);
62+
sleep(2);
63+
}
64+
65+
public function testListEntries()
66+
{
67+
$application = new Application();
68+
$application->add(new ListEntriesCommand());
69+
$commandTester = new CommandTester($application->get('list-entries'));
70+
$commandTester->execute(
71+
['--project' => $this->projectId, '--logger' => 'my_test_logger'],
72+
['interactive' => false]
73+
);
74+
$this->expectOutputRegex('/: Test Message/');
75+
}
76+
}

0 commit comments

Comments
 (0)