1
0
mirror of https://github.com/microsoft/TypeScript-Node-Starter.git synced 2025-11-09 00:37:27 +00:00

replaced logging with Winston

This commit is contained in:
Bowden
2018-01-04 18:06:40 -08:00
parent 5e762331d8
commit 1a72f0db6b
5 changed files with 128 additions and 26 deletions

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