1
0
mirror of https://github.com/microsoft/TypeScript-Node-Starter.git synced 2026-03-23 21:51:44 +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

@@ -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);
}