1
0
mirror of https://github.com/microsoft/TypeScript-Node-Starter.git synced 2026-03-23 10:41:42 +00:00

replaced logging with Winston

This commit is contained in:
Bowden
2018-01-04 18:06:40 -08:00
parent d822f12121
commit 31f3963b76
7 changed files with 106 additions and 44 deletions

View File

@@ -2,7 +2,7 @@ import * as express from "express";
import * as compression from "compression"; // compresses requests
import * as session from "express-session";
import * as bodyParser from "body-parser";
import * as logger from "morgan";
import * as logger from "winston";
import * as lusca from "lusca";
import * as dotenv from "dotenv";
import * as mongo from "connect-mongo";
@@ -12,7 +12,7 @@ import * as mongoose from "mongoose";
import * as passport from "passport";
import * as expressValidator from "express-validator";
import * as bluebird from "bluebird";
import { MONGODB_URI, SESSION_SECRET } from "./util/loadSecrets";
import { MONGODB_URI, SESSION_SECRET } from "./util/secrets";
const MongoStore = mongo(session);
@@ -43,7 +43,6 @@ app.set("port", process.env.PORT || 3000);
app.set("views", path.join(__dirname, "../views"));
app.set("view engine", "pug");
app.use(compression());
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());

View File

@@ -1,37 +0,0 @@
// import log from './logger'; TODO
import * as dotenv from "dotenv";
export const ENVIRONMENT = process.env.NODE_ENV;
const prod = ENVIRONMENT === "production"; // Anything else is treated as 'dev'
dotenv.config({ path: ".env.example" });
export const SESSION_SECRET = process.env["SESSION_SECRET"];
export const MONGODB_URI = prod ? process.env["MONGODB_URI"] : process.env["MONGODB_URI_LOCAL"];
// if (!GITHUB_CLIENT_SECRET) {
// log.fatal("ERROR\tNo client secret. Set TCQ_GH_SECRET.");
// process.exit(1);
// }
// if (!GITHUB_CLIENT_ID) {
// log.fatal("ERROR\tNo client id. Set TCQ_GH_ID.");
// process.exit(1);
// }
// if (!SESSION_SECRET) {
// log.fatal("ERROR\tNo session secret. Set TCQ_SESSION_SECRET.");
// process.exit(1);
// }
// if (!CDB_SECRET) {
// log.fatal("ERROR\tNo CosmosDB secret. Set TCQ_CDB_SECRET.");
// process.exit(1);
// }
// if (!AI_IKEY) {
// log.fatal("ERROR\tNo Application Insights Instrumentation Key. Set TCQ_AI_IKEY.");
// process.exit(1);
// }

16
src/util/logger.ts Normal file
View File

@@ -0,0 +1,16 @@
import * as winston from "winston";
import { ENVIRONMENT } from "./secrets";
const logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ level: process.env.NODE_ENV === "production" ? "error" : "debug" }),
new (winston.transports.File)({ filename: "debug.log", level: "debug"})
]
});
if (process.env.NODE_ENV !== "production") {
logger.debug("Logging initialized at debug level");
}
export default logger;

26
src/util/secrets.ts Normal file
View File

@@ -0,0 +1,26 @@
import logger from "./logger";
import * as dotenv from "dotenv";
import * as fs from "fs";
if (fs.existsSync(".env")) {
logger.debug("Using .env file to supply config environment variables");
dotenv.config({ path: ".env" });
} else {
logger.debug("Using .env.example file to supply config environment variables");
dotenv.config({ path: ".env.example" }); // you can delete this after you create your own .env file!
}
export const ENVIRONMENT = process.env.NODE_ENV;
const prod = ENVIRONMENT === "production"; // Anything else is treated as 'dev'
export const SESSION_SECRET = process.env["SESSION_SECRET"];
export const MONGODB_URI = prod ? process.env["MONGODB_URI"] : process.env["MONGODB_URI_LOCAL"];
if (!SESSION_SECRET) {
logger.error("No client secret. Set SESSION_SECRET environment variable.");
process.exit(1);
}
if (!MONGODB_URI) {
logger.error("No mongo connection string. Set MONGODB_URI environment variable.");
process.exit(1);
}