|
| 1 | +/*jshint unused:false*/ |
| 2 | +// Copyright 2015-2016, Google, Inc. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +// [START app] |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +var express = require('express'); |
| 20 | +var winston = require('winston'); |
| 21 | + |
| 22 | +var app = express(); |
| 23 | +var logFile = '/var/log/app_engine/custom_logs/myapp.errors.log.json'; |
| 24 | + |
| 25 | +winston.add(winston.transports.File, { |
| 26 | + filename: logFile |
| 27 | +}); |
| 28 | + |
| 29 | +function report(err, req) { |
| 30 | + var payload = { |
| 31 | + serviceContext: { |
| 32 | + service: 'myapp', |
| 33 | + }, |
| 34 | + message: err.stack, |
| 35 | + context: { |
| 36 | + httpRequest: { |
| 37 | + url: req.originalUrl, |
| 38 | + method: req.method, |
| 39 | + referrer: req.header('Referer'), |
| 40 | + userAgent: req.header('User-Agent'), |
| 41 | + remoteIp: req.ip, |
| 42 | + responseStatusCode: 500, |
| 43 | + } |
| 44 | + } |
| 45 | + }; |
| 46 | + winston.error(payload); |
| 47 | +} |
| 48 | + |
| 49 | +app.get('/', function (req, res, next) { |
| 50 | + next(new Error('something is wrong!')); |
| 51 | +}); |
| 52 | + |
| 53 | +app.use(function (err, req, res, next) { |
| 54 | + report(err, req); |
| 55 | + res.status(500).send(err.message || 'Something broke!'); |
| 56 | +}); |
| 57 | + |
| 58 | +var server = app.listen(process.env.PORT || '8080', function () { |
| 59 | + console.log('App listening on port %s', server.address().port); |
| 60 | + console.log('Press Ctrl+C to quit.'); |
| 61 | +}); |
| 62 | +// [END app] |
0 commit comments