Skip to content

Commit 660a460

Browse files
authored
chore: update logging to new samples format (#1643)
1 parent b2a3e07 commit 660a460

14 files changed

+336
-437
lines changed

logging/README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@
88
This directory contains samples for calling [Stackdriver Logging][logging]
99
from PHP.
1010

11-
`logging.php` is a simple command-line program to demonstrate writing to a log,
12-
listing its entries, deleting it, interacting with sinks to export logs to
13-
Google Cloud Storage.
11+
Execute the snippets in the [src/](src/) directory by running
12+
`php src/SNIPPET_NAME.php`. The usage will print for each if no arguments
13+
are provided:
14+
```sh
15+
$ php src/list_entries.php
16+
Usage: php src/list_entries.php PROJECT_ID LOGGER_NAME
17+
18+
$ php src/list_entries.php your-project-id 'your-logger-name'
19+
[list of entries...]
20+
```
1421

1522
To use logging sinks, you will also need a Google Cloud Storage Bucket.
1623

@@ -27,11 +34,4 @@ Use the [Cloud SDK](https://cloud.google.com/sdk) to provide authentication:
2734

2835
gcloud beta auth application-default login
2936

30-
Run the samples:
31-
32-
```
33-
php logging.php list # For getting sub command list
34-
php logging.php help write # For showing help for write sub command `write`
35-
```
36-
3737
[logging]: https://cloud.google.com/logging/docs/reference/libraries

logging/composer.json

-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
{
22
"require": {
33
"google/cloud-logging": "^1.20.0",
4-
"symfony/console": "^5.0",
54
"monolog/monolog": "^2.0"
6-
},
7-
"autoload": {
8-
"files": [
9-
"src/log_entry_functions.php",
10-
"src/write_with_psr_logger.php",
11-
"src/write_with_monolog_logger.php",
12-
"src/sink_functions.php"
13-
]
145
}
156
}

logging/logging.php

-232
This file was deleted.

logging/src/create_sink.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
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 logging_create_sink]
21+
use Google\Cloud\Logging\LoggingClient;
22+
23+
/**
24+
* Create a log sink.
25+
*
26+
* @param string $projectId The Google project ID.
27+
* @param string $sinkName The name of the sink.
28+
* @param string $destination The destination of the sink.
29+
* @param string $filterString The filter for the sink.
30+
*/
31+
function create_sink($projectId, $sinkName, $destination, $filterString)
32+
{
33+
$logging = new LoggingClient(['projectId' => $projectId]);
34+
$logging->createSink(
35+
$sinkName,
36+
$destination,
37+
['filter' => $filterString]
38+
);
39+
printf("Created a sink '%s'." . PHP_EOL, $sinkName);
40+
}
41+
// [END logging_create_sink]
42+
43+
// The following 2 lines are only needed to run the samples
44+
require_once __DIR__ . '/../../testing/sample_helpers.php';
45+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

logging/src/delete_logger.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
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 logging_delete_log]
21+
use Google\Cloud\Logging\LoggingClient;
22+
23+
/** Delete a logger and all its entries.
24+
*
25+
* @param string $projectId The Google project ID.
26+
* @param string $loggerName The name of the logger.
27+
*/
28+
function delete_logger($projectId, $loggerName)
29+
{
30+
$logging = new LoggingClient(['projectId' => $projectId]);
31+
$logger = $logging->logger($loggerName);
32+
$logger->delete();
33+
printf("Deleted a logger '%s'." . PHP_EOL, $loggerName);
34+
}
35+
// [END logging_delete_log]
36+
37+
// The following 2 lines are only needed to run the samples
38+
require_once __DIR__ . '/../../testing/sample_helpers.php';
39+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)