-
Notifications
You must be signed in to change notification settings - Fork 13
/
errors.js
40 lines (32 loc) · 1.14 KB
/
errors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const express = require('express');
const app = express();
// set up Stackdriver logging
const bunyan = require('bunyan');
// Imports the Google Cloud client library for Bunyan
const {LoggingBunyan} = require('@google-cloud/logging-bunyan');
// Creates a Bunyan Stackdriver Logging client
const loggingBunyan = new LoggingBunyan();
// Create a Bunyan logger that streams to Stackdriver Logging
// Logs will be written to: "projects/YOUR_PROJECT_ID/logs/bunyan_log"
const logger = bunyan.createLogger({
name: 'node-example',
streams: [
// Log to the console at 'info' and above
{stream: process.stdout, level: 'info'},
// And log to Stackdriver Logging, logging at 'info' and above
loggingBunyan.stream('info'),
],
});
app.get('/', (req, res) => {
console.log("request made");
// log error 20% of the time
if ((Math.floor(Math.random() * 100)) <= 20) {
// log error
logger.error("failure!");
// return error code
res.status(500).send("error!")
}
logger.info("success!");
res.status(200).send("success!");
})
app.listen(8080, () => console.log(`Example app listening on port 8080!`))