forked from oat/in-the-database-2
		
	set up logger
This commit is contained in:
		
							parent
							
								
									ca0852800e
								
							
						
					
					
						commit
						dec7600b92
					
				
					 4 changed files with 220 additions and 9 deletions
				
			
		
							
								
								
									
										36
									
								
								src/index.ts
									
										
									
									
									
								
							
							
						
						
									
										36
									
								
								src/index.ts
									
										
									
									
									
								
							|  | @ -1,6 +1,9 @@ | |||
| import * as express from 'express'; | ||||
| import * as mongoose from 'mongoose'; | ||||
| import * as fs from 'fs'; | ||||
| import * as winston from 'winston'; | ||||
| 
 | ||||
| import * as format from './lib/format'; | ||||
| 
 | ||||
| const config = JSON.parse(fs.readFileSync('./config/config.json', {encoding: 'utf8'})); | ||||
| 
 | ||||
|  | @ -11,9 +14,31 @@ const dbClient = mongoose.connect(`${config.dbconnectionURL}/${config.dbname}`, | |||
|   useCreateIndex: true | ||||
| }); | ||||
| 
 | ||||
| console.log('connecting to mongodb database...'); | ||||
| const logger = winston.createLogger({ | ||||
| 	level: 'info', | ||||
| 	format: winston.format.combine( | ||||
| 		winston.format.timestamp(), | ||||
| 		winston.format.printf(log => `${format.formatTime(new Date(log.timestamp))} | ${log.message}`) | ||||
| 	), | ||||
| 	transports: [ | ||||
| 		new winston.transports.File({filename: `${config.name}-error.log`, level: 'error'}), | ||||
| 		new winston.transports.File({filename: `${config.name}.log`}), | ||||
| 		new winston.transports.Console({ | ||||
| 			format: winston.format.combine( | ||||
| 				winston.format.colorize(), | ||||
| 				winston.format.timestamp(), | ||||
| 				winston.format.printf(log =>  | ||||
| 					`${format.formatTime(new Date(log.timestamp))} - [${log.level}] ${log.message}` | ||||
| 				) | ||||
| 			), | ||||
| 			level: process.env.DEBUG === 'true' ? 'silly' : 'info' | ||||
| 		}) | ||||
| 	] | ||||
| }); | ||||
| 
 | ||||
| logger.info('connecting to mongodb database'); | ||||
| dbClient.then(() => { | ||||
| 	console.log('connected to database!'); | ||||
| 	logger.info('connected to database!'); | ||||
| 
 | ||||
| 	const app = express(); | ||||
| 
 | ||||
|  | @ -21,20 +46,17 @@ dbClient.then(() => { | |||
| 
 | ||||
| 	app.set('db', dbClient); | ||||
| 	app.set('config', config); | ||||
| 	app.set('logger', logger); | ||||
| 
 | ||||
| 	app.get('/', (req, res) => { | ||||
| 		res.send(`${config.name} homepage - unfinished`); | ||||
| 	}); | ||||
| 
 | ||||
| 	app.get('/recent', (req, res) => { | ||||
| 		let query = req.query; | ||||
| 	}); | ||||
| 
 | ||||
| 	app.get('*', (req, res) => { | ||||
| 		res.status(404).send('404'); | ||||
| 	}); | ||||
| 
 | ||||
| 	app.listen(config.port, () => { | ||||
| 		console.log(`expressjs server launched on port ${config.port}, should now function properly`); | ||||
| 		logger.info(`expressjs server launched on port ${config.port}, should now function properly`); | ||||
| 	}); | ||||
| }); | ||||
							
								
								
									
										6
									
								
								src/lib/format.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								src/lib/format.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,6 @@ | |||
| export function formatTime(date: Date) : string { | ||||
| 	let hours = date.getUTCHours(); | ||||
| 	let minutes = date.getUTCMinutes(); | ||||
| 
 | ||||
| 	return `${hours < 10 ? '0' : ''}${hours}:${minutes < 10 ? '0' : ''}${minutes} UTC`; | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue