You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 11, 2026. It is now read-only.
Copy file name to clipboardExpand all lines: .readme-partials.yml
+55Lines changed: 55 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -71,3 +71,58 @@ body: |-
71
71
72
72
If you already have a "raw" Http `request` object you can assign it to `entry.metadata.httpRequest` directly. More information about
73
73
how the `request` is interpreted as raw can be found in the [code](https://github.com/googleapis/nodejs-logging/blob/15849160116a814ab71113138cb211c2e0c2d4b4/src/entry.ts#L224-L238).
74
+
75
+
## Error handling with logs written or deleted asynchronously
76
+
77
+
The `Log` class provide users the ability to write and delete logs asynchronously. However, there are cases when log entries
78
+
cannot be written or deleted and error is thrown - if error is not handled properly, it could crash the application.
79
+
One possible way to catch the error is to `await` the log write/delete calls and wrap it with `try/catch` like in example below:
80
+
81
+
```js
82
+
// Write log entry and and catch any errors
83
+
try {
84
+
await log.write(entry);
85
+
} catch (err) {
86
+
console.log('Error is: ' + err);
87
+
}
88
+
```
89
+
90
+
However, awaiting for every `log.write` or `log.delete` calls may introduce delays which could be avoided by
91
+
simply adding a callback like in the example below. This way the log entry can be queued for processing and code
92
+
execution will continue without further delays. The callback will be called once the operation is complete:
93
+
94
+
```js
95
+
// Asynchronously write the log entry and handle respone or any errors in provided callback
96
+
log.write(entry, err => {
97
+
if (err) {
98
+
// The log entry was not written.
99
+
console.log(err.message);
100
+
} else {
101
+
console.log('No error in write callback!');
102
+
}
103
+
});
104
+
```
105
+
106
+
Adding a callback to every `log.write` or `log.delete` calls could be a burden, especially if code
107
+
handling the error is always the same. For this purpose we introduced an ability to provide a default callback
108
+
for `Log` class which could be set through `LogOptions` passed to `Log` constructor as in example below - this
109
+
way you can define a global callback once for all `log.write` and `log.delete` calls and be able to handle errors:
// Create options with default callback to be called on every write/delete response or error
116
+
const options = {
117
+
defaultWriteDeleteCallback: function (err) {
118
+
if (err) {
119
+
console.log('Error is: ' + err);
120
+
} else {
121
+
console.log('No error, all is good!');
122
+
}
123
+
},
124
+
};
125
+
126
+
const log = logging.log('my-log', options);
127
+
```
128
+
See the full sample in `writeLogWithCallback` function [here](https://github.com/googleapis/nodejs-logging/blob/master/samples/logs.js).
Copy file name to clipboardExpand all lines: README.md
+55Lines changed: 55 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -162,6 +162,61 @@ how to populate the Http request metadata for log entries.
162
162
If you already have a "raw" Http `request` object you can assign it to `entry.metadata.httpRequest` directly. More information about
163
163
how the `request` is interpreted as raw can be found in the [code](https://github.com/googleapis/nodejs-logging/blob/15849160116a814ab71113138cb211c2e0c2d4b4/src/entry.ts#L224-L238).
164
164
165
+
## Error handling with logs written or deleted asynchronously
166
+
167
+
The `Log` class provide users the ability to write and delete logs asynchronously. However, there are cases when log entries
168
+
cannot be written or deleted and error is thrown - if error is not handled properly, it could crash the application.
169
+
One possible way to catch the error is to `await` the log write/delete calls and wrap it with `try/catch` like in example below:
170
+
171
+
```js
172
+
// Write log entry and and catch any errors
173
+
try {
174
+
awaitlog.write(entry);
175
+
} catch (err) {
176
+
console.log('Error is: '+ err);
177
+
}
178
+
```
179
+
180
+
However, awaiting for every `log.write` or `log.delete` calls may introduce delays which could be avoided by
181
+
simply adding a callback like in the example below. This way the log entry can be queued for processing and code
182
+
execution will continue without further delays. The callback will be called once the operation is complete:
183
+
184
+
```js
185
+
// Asynchronously write the log entry and handle respone or any errors in provided callback
186
+
log.write(entry, err=> {
187
+
if (err) {
188
+
// The log entry was not written.
189
+
console.log(err.message);
190
+
} else {
191
+
console.log('No error in write callback!');
192
+
}
193
+
});
194
+
```
195
+
196
+
Adding a callback to every `log.write` or `log.delete` calls could be a burden, especially if code
197
+
handling the error is always the same. For this purpose we introduced an ability to provide a default callback
198
+
for `Log` class which could be set through `LogOptions` passed to `Log` constructor as in example below - this
199
+
way you can define a global callback once for all `log.write` and `log.delete` calls and be able to handle errors:
// Create options with default callback to be called on every write/delete response or error
206
+
constoptions= {
207
+
defaultWriteDeleteCallback:function (err) {
208
+
if (err) {
209
+
console.log('Error is: '+ err);
210
+
} else {
211
+
console.log('No error, all is good!');
212
+
}
213
+
},
214
+
};
215
+
216
+
constlog=logging.log('my-log', options);
217
+
```
218
+
See the full sample in `writeLogWithCallback` function [here](https://github.com/googleapis/nodejs-logging/blob/master/samples/logs.js).
0 commit comments