-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.coffee
More file actions
56 lines (50 loc) · 1.61 KB
/
index.coffee
File metadata and controls
56 lines (50 loc) · 1.61 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
fs = require 'fs'
path = require('path')
winston = require('winston')
require('winston-daily-rotate-file')
Sentry = require 'winston-raven-sentry'
morgan = require('morgan')
rfs = require('rotating-file-stream')
_setupWinston = ()->
# setup the daily rotate log
dailyRotateTransport = new (winston.transports.DailyRotateFile)(
filename: "#{process.env.LOG_PATH}/log"
datePattern: 'yyyy-MM-dd.'
prepend: true
level: process.env.LOG_LEVEL
)
# setup the basic transports
transports = [
dailyRotateTransport
]
# setup the console transport for dev
if process.env.NODE_ENV != 'production'
transports.push new winston.transports.Console
level: process.env.LOG_LEVEL
# setup sentry if a dsn is provided
if process.env.SENTRY_DSN
transports.push new Sentry
dsn: process.env.SENTRY_DSN
level: 'error'
# create the logger
logger = new (winston.Logger)
transports: transports
# make the logger available as global Winston
global['Winston'] = logger
# finally, log that we got a logger
Winston.silly '[Winston]: initialized'
# return the logger
return logger
_setupMorgan = () ->
format = if process.env.NODE_ENV == 'production' then 'combined' else 'short'
accessLogStream = rfs 'access.log',
interval: '1d',
path: process.env.LOG_PATH
return morgan(format, {stream: accessLogStream})
module.exports = (app) ->
# ensure the directory exists
fs.existsSync(process.env.LOG_PATH) || fs.mkdirSync(process.env.LOG_PATH)
# setup the winston logger
logger = _setupWinston()
# setup morgan and use it as express middleware
app.use _setupMorgan()