13
13
14
14
'use strict' ;
15
15
16
- // [START all]
17
16
// [START setup]
18
17
// By default, the client will authenticate using the service account file
19
18
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
20
19
// the project specified by the GCLOUD_PROJECT environment variable. See
21
- // https://googlecloudplatform.github.io/gcloud -node/#/docs/google-cloud/latest/guides/authentication
20
+ // https://googlecloudplatform.github.io/google-cloud -node/#/docs/google-cloud/latest/guides/authentication
22
21
var Logging = require ( '@google-cloud/logging' ) ;
23
-
24
- // Instantiate a logging client
25
- var logging = Logging ( ) ;
26
22
// [END setup]
27
23
28
- // [START list_log_entries]
29
- /**
30
- * List log entires in the authenticated project.
31
- *
32
- * @param {object } [options] Optional. Configuration options.
33
- * @param {string } [options.filter] Optional. An advanced logs filter. An empty filter matches all log entries.
34
- * @param {number } [options.limit] Optional. Maximum number of logs to return.
35
- * @param {string } [options.sort] Optional. Default: "timestamp desc".
36
- * @param {function } callback The callback function.
37
- */
38
- function listLogEntries ( options , callback ) {
39
- // Configuration to pass to Logging#getEntries
40
- var config = { } ;
41
-
42
- if ( options . filter ) {
43
- config . filter = options . filter ;
44
- }
45
- if ( options . limit ) {
46
- config . pageSize = options . limit ;
47
- }
48
- if ( options . sort ) {
49
- config . orderBy = options . sort ;
50
- }
24
+ function writeLogEntry ( logName , callback ) {
25
+ var logging = Logging ( ) ;
26
+ var log = logging . log ( logName ) ;
27
+
28
+ // Modify this resource to match a resource in your project
29
+ // See https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource
30
+ var resource = {
31
+ // This example targets the "global" resource for simplicity
32
+ type : 'global'
33
+ } ;
34
+
35
+ // A text log entry
36
+ var entry = log . entry ( resource , 'Hello, world!' ) ;
37
+
38
+ // A structured log entry
39
+ var secondEntry = log . entry ( resource , {
40
+ name : 'King Arthur' ,
41
+ quest : 'Find the Holy Grail' ,
42
+ favorite_color : 'Blue'
43
+ } ) ;
51
44
52
- // See https://googlecloudplatform.github.io/gcloud-node/#/docs/logging/latest/logging
53
- logging . getEntries ( config , function ( err , entries ) {
45
+ // Save the two log entries. You can write entries one at a time, but it is
46
+ // best to write multiple entires together in a batch.
47
+ log . write ( [
48
+ entry ,
49
+ secondEntry
50
+ ] , function ( err , apiResponse ) {
54
51
if ( err ) {
55
52
return callback ( err ) ;
56
53
}
57
54
58
- console . log ( 'Found %d entries! ' , entries . length ) ;
59
- return callback ( null , entries ) ;
55
+ console . log ( 'Wrote to %s ' , logName ) ;
56
+ return callback ( null , apiResponse ) ;
60
57
} ) ;
61
58
}
62
- // [END list_log_entries]
63
-
64
- // [START write_log_entry]
65
- /**
66
- * Write a log entry.
67
- *
68
- * @param {object } options Configuration options.
69
- * @param {string } options.name The name of the log to write to.
70
- * @param {object } options.resource The resource to associate with the log entry.
71
- * @param {string|object } options.entry The body of the log entry.
72
- * @param {function } callback The callback function.
73
- */
74
- function writeLogEntry ( options , callback ) {
75
- // Get a reference to an existing log
76
- var log = logging . log ( options . name ) ;
59
+
60
+ function writeLogEntryAdvanced ( logName , options , callback ) {
61
+ var logging = Logging ( ) ;
62
+ var log = logging . log ( logName ) ;
77
63
78
64
// Prepare the entry
79
65
var entry = log . entry ( options . resource , options . entry ) ;
80
66
81
- // See https://googlecloudplatform.github.io/gcloud-node/#/docs/logging/latest/logging/log
82
- log . write ( entry , function ( err ) {
67
+ // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/log?method=write
68
+ log . write ( entry , function ( err , apiResponse ) {
69
+ if ( err ) {
70
+ return callback ( err ) ;
71
+ }
72
+
73
+ console . log ( 'Wrote entry to log: %s' , logName ) ;
74
+ return callback ( null , apiResponse ) ;
75
+ } ) ;
76
+ }
77
+
78
+ function listLogEntries ( logName , callback ) {
79
+ var logging = Logging ( ) ;
80
+ var log = logging . log ( logName ) ;
81
+
82
+ // List the most recent entries for a given log
83
+ // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getEntries
84
+ log . getEntries ( function ( err , entries ) {
85
+ if ( err ) {
86
+ return callback ( err ) ;
87
+ }
88
+
89
+ console . log ( 'Found %d entries!' , entries . length ) ;
90
+ return callback ( null , entries ) ;
91
+ } ) ;
92
+ }
93
+
94
+ function listLogEntriesAdvanced ( filter , limit , sort , callback ) {
95
+ var logging = Logging ( ) ;
96
+ var options = { } ;
97
+
98
+ if ( filter ) {
99
+ // See https://cloud.google.com/logging/docs/view/advanced_filters for more filter information.
100
+ options . filter = filter ;
101
+ }
102
+ if ( limit ) {
103
+ options . pageSize = limit ;
104
+ }
105
+ if ( sort ) {
106
+ options . orderBy = sort ;
107
+ }
108
+
109
+ // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging?method=getEntries
110
+ logging . getEntries ( options , function ( err , entries ) {
83
111
if ( err ) {
84
112
return callback ( err ) ;
85
113
}
86
114
87
- console . log ( 'Wrote entry to log: %s ' , options . name ) ;
88
- callback ( null ) ;
115
+ console . log ( 'Found %d entries! ' , entries . length ) ;
116
+ return callback ( null , entries ) ;
89
117
} ) ;
90
118
}
91
- // [END write_log_entry]
92
-
93
- // [START delete_log]
94
- /**
95
- * Delete a log.
96
- *
97
- * @param {string } name The name of the log to delete.
98
- * @param {function } callback The callback function.
99
- */
100
- function deleteLog ( name , callback ) {
101
- // Get a reference to the Log to be deleted
102
- var log = logging . log ( name ) ;
103
-
104
- // Delete the log
105
- log . delete ( function ( err ) {
119
+
120
+ function deleteLog ( logName , callback ) {
121
+ var logging = Logging ( ) ;
122
+ var log = logging . log ( logName ) ;
123
+
124
+ // Deletes a logger and all its entries.
125
+ // Note that a deletion can take several minutes to take effect.
126
+ // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/log?method=delete
127
+ log . delete ( function ( err , apiResponse ) {
106
128
if ( err ) {
107
129
return callback ( err ) ;
108
130
}
109
131
110
- console . log ( 'Deleted log: %s' , name ) ;
111
- callback ( null ) ;
132
+ console . log ( 'Deleted log: %s' , logName ) ;
133
+ return callback ( null , apiResponse ) ;
112
134
} ) ;
113
135
}
114
- // [END delete_log]
115
- // [END all]
116
136
117
137
// The command-line program
118
138
var cli = require ( 'yargs' ) ;
119
139
var utils = require ( '../utils' ) ;
120
140
121
141
var program = module . exports = {
122
- listLogEntries : listLogEntries ,
123
142
writeLogEntry : writeLogEntry ,
143
+ writeLogEntryAdvanced : writeLogEntryAdvanced ,
144
+ listLogEntries : listLogEntries ,
145
+ listLogEntriesAdvanced : listLogEntriesAdvanced ,
124
146
deleteLog : deleteLog ,
125
147
main : function ( args ) {
126
148
// Run the command-line program
@@ -130,7 +152,7 @@ var program = module.exports = {
130
152
131
153
cli
132
154
. demand ( 1 )
133
- . command ( 'list' , 'List log entries.' , {
155
+ . command ( 'list' , 'Lists log entries, optionally filtering, limiting, and sorting results .' , {
134
156
filter : {
135
157
alias : 'f' ,
136
158
type : 'string' ,
@@ -150,29 +172,31 @@ cli
150
172
description : 'Sort results.'
151
173
}
152
174
} , function ( options ) {
153
- program . listLogEntries ( utils . pick ( options , [ ' filter' , ' limit' , ' sort' ] ) , utils . makeHandler ( ) ) ;
175
+ program . listLogEntriesAdvanced ( options . filter , options . limit , options . sort , utils . makeHandler ( ) ) ;
154
176
} )
155
- . command ( 'write <name > <resource> <entry>' , 'Write a log entry.' , { } , function ( options ) {
177
+ . command ( 'write <logName > <resource> <entry>' , 'Writes a log entry to the specified log .' , { } , function ( options ) {
156
178
try {
157
179
options . resource = JSON . parse ( options . resource ) ;
158
180
} catch ( err ) {
159
- console . error ( '"resource" must be a valid JSON string!' ) ;
181
+ return console . error ( '"resource" must be a valid JSON string!' ) ;
160
182
}
183
+
161
184
try {
162
185
options . entry = JSON . parse ( options . entry ) ;
163
- } catch ( err ) {
164
- return console . error ( '"entry" must be a valid JSON string!' ) ;
165
- }
166
- program . writeLogEntry ( utils . pick ( options , [ 'name' , 'resource' , 'entry' ] ) , utils . makeHandler ( ) ) ;
186
+ } catch ( err ) { }
187
+
188
+ program . writeLogEntryAdvanced ( options . logName , utils . pick ( options , [ 'resource' , 'entry' ] ) , utils . makeHandler ( false ) ) ;
167
189
} )
168
- . command ( 'delete <name >' , 'Delete a Log.' , { } , function ( options ) {
169
- program . deleteLog ( options . name , utils . makeHandler ( false ) ) ;
190
+ . command ( 'delete <logName >' , 'Deletes the specified Log.' , { } , function ( options ) {
191
+ program . deleteLog ( options . logName , utils . makeHandler ( false ) ) ;
170
192
} )
171
193
. example ( 'node $0 list' , 'List all log entries.' )
172
- . example ( 'node $0 list -f "severity = ERROR" -s "timestamp" -l 2' , 'List up to 2 error entries, sorted by timestamp ascending.' )
173
- . example ( 'node $0 write my-log \'{"type":"gae_app","labels":{"module_id":"default"}}\' \'{"message":"Hello World!"}\'' , 'Write a log entry.' )
194
+ . example ( 'node $0 list -f "severity=ERROR" -s "timestamp" -l 2' , 'List up to 2 error entries, sorted by timestamp ascending.' )
195
+ . example ( 'node $0 list -f \'logName="my-log"\' -l 2' , 'List up to 2 log entries from the "my-log" log.' )
196
+ . example ( 'node $0 write my-log \'{"type":"gae_app","labels":{"module_id":"default"}}\' \'"Hello World!"\'' , 'Write a string log entry.' )
197
+ . example ( 'node $0 write my-log \'{"type":"global"}\' \'{"message":"Hello World!"}\'' , 'Write a JSON log entry.' )
174
198
. example ( 'node $0 delete my-log' , 'Delete "my-log".' )
175
- . wrap ( 100 )
199
+ . wrap ( 120 )
176
200
. recommendCommands ( )
177
201
. epilogue ( 'For more information, see https://cloud.google.com/logging/docs' ) ;
178
202
0 commit comments