UNPKG

app-logging

Version:
121 lines (120 loc) 5.33 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); var index_1 = require("../src/index"); var chai_1 = require("chai"); var fs = __importStar(require("fs")); var storagePath = process.env.PWD + '/storage'; describe('App Logging Test', function () { beforeEach(function () { // Remove storage folder if (fs.existsSync(storagePath)) removeDir(storagePath); }); it('should save info log in /storage/app.log file', function () { // Write log var logging = new index_1.Log(); logging.info('info log message', 'info log contextualData'); // Read Log File var data = fs.readFileSync(storagePath + "/app.log", 'utf8'); chai_1.expect(data.includes('.INFO: info log message info log contextualData')).to.be.true; }); it('should save error log in /storage/app.log file', function () { // Write log var logging = new index_1.Log(); logging.error('error log message', 'error log contextualData'); // Read Log File var data = fs.readFileSync(storagePath + "/app.log", 'utf8'); chai_1.expect(data.includes('.ERROR: error log message error log contextualData')).to.be.true; }); it('should save warning log in /storage/app.log file', function () { // Write log var logging = new index_1.Log(); logging.warning('warning log message', 'warning log contextualData'); // Read Log File var data = fs.readFileSync(storagePath + "/app.log", 'utf8'); chai_1.expect(data.includes('.WARNING: warning log message warning log contextualData')).to.be.true; }); it('should save alert log in /storage/app.log file', function () { // Write log var logging = new index_1.Log(); logging.alert('alert log message', 'alert log contextualData'); // Read Log File var data = fs.readFileSync(storagePath + "/app.log", 'utf8'); chai_1.expect(data.includes('.ALERT: alert log message alert log contextualData')).to.be.true; }); it('should save notice log in /storage/app.log file', function () { // Write log var logging = new index_1.Log(); logging.notice('notice log message', 'notice log contextualData'); // Read Log File var data = fs.readFileSync(storagePath + "/app.log", 'utf8'); chai_1.expect(data.includes('.NOTICE: notice log message notice log contextualData')).to.be.true; }); it('should save debug log in /storage/app.log file', function () { // Write log var logging = new index_1.Log(); logging.debug('debug log message', 'debug log contextualData'); // Read Log File var data = fs.readFileSync(storagePath + "/app.log", 'utf8'); chai_1.expect(data.includes('.DEBUG: debug log message debug log contextualData')).to.be.true; }); it('should save emergency log in /storage/app.log file', function () { // Write log var logging = new index_1.Log(); logging.emergency('emergency log message', 'emergency log contextualData'); // Read Log File var data = fs.readFileSync(storagePath + "/app.log", 'utf8'); chai_1.expect(data.includes('.EMERGENCY: emergency log message emergency log contextualData')).to.be.true; }); it('should save critical log in /storage/app.log file', function () { // Write log var logging = new index_1.Log(); logging.critical('critical log message', 'critical log contextualData'); // Read Log File var data = fs.readFileSync(storagePath + "/app.log", 'utf8'); chai_1.expect(data.includes('.CRITICAL: critical log message critical log contextualData')).to.be.true; }); afterEach(function () { // Remove storage folder if (fs.existsSync(storagePath)) removeDir(storagePath); }); }); var removeDir = function (path) { if (fs.existsSync(path)) { var files = fs.readdirSync(path); if (files.length > 0) { files.forEach(function (filename) { if (fs.statSync(path + "/" + filename).isDirectory()) { removeDir(path + "/" + filename); } else { fs.unlinkSync(path + "/" + filename); } }); fs.rmdirSync(path); } else { fs.rmdirSync(path); } } };