Kaydet (Commit) 89243ee8 authored tarafından Ali's avatar Ali

Added send method and ServerException for LogServer

üst 1217d8ba
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
......@@ -9,30 +10,61 @@ var __assign = (this && this.__assign) || function () {
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var ServerException_1 = require("./Exceptions/ServerException");
/**
* @class LogServer
* @description This class helps to debug messages send to the server.
*/
var LogServer = /** @class */ (function () {
/**
*
* @param serverSettings Log backend settings as a parameter
*/
function LogServer(serverSettings) {
/**
* Log Backend Settings
*/
this.settings = {
useServer: false,
endPoint: '',
method: 'POST',
headers: {},
body: {}
};
this.settings = __assign({}, serverSettings);
if (this.settings.useServer) {
if (!this.settings.endPoint) {
throw new ServerException_1.default("Endpoint must be defined!");
}
}
}
LogServer.prototype.sendToLogServer = function (sendBody) {
/**
* @description private function to send data to log backend
* @function sendToServer
* @param sendBody
*/
LogServer.prototype.sendToServer = function (sendBody) {
var endPoint = this.settings.endPoint;
var headers = this.settings.headers;
var body = this.settings.body;
var method = this.settings.method;
fetch(endPoint, {
return fetch(endPoint, {
method: method,
headers: __assign({}, headers),
body: body
body: JSON.stringify(__assign({}, body, sendBody))
});
};
/**
* @description public function to send data to log backend. If useServer flag is not true, data will not send to the server.
* @function sendToLogServer
* @param sendBody
*/
LogServer.prototype.sendToLogServer = function (sendBody) {
if (this.settings.useServer) {
this.sendToServer(sendBody);
}
};
return LogServer;
}());
exports.default = LogServer;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var LogTypes_1 = require("./LogTypes");
var LogServer_1 = require("./LogServer");
/**
* @description Logger class. This class will have logging functions
* @class Logger
......@@ -8,10 +9,25 @@ var LogTypes_1 = require("./LogTypes");
var Logger = /** @class */ (function () {
function Logger(logConfig) {
this.config = {
server: {}
useServer: false,
endPoint: '',
method: 'POST',
headers: {},
body: {}
};
this.config = logConfig;
this.server = new LogServer_1.default(this.config.server);
}
/**
* @description Send extra fields to the log backend
* @function send
* @param sendBody Extra body field
* @example
* loggerInstance.send({ pageUrl: 'test', 'time': '12:30' })
*/
Logger.prototype.send = function (sendBody) {
return this.server.sendToLogServer(sendBody);
};
/**
* @description Debug Level Message
* @function debug
......@@ -64,6 +80,58 @@ var Logger = /** @class */ (function () {
var levelName = LogTypes_1.Levels.GET(logCode);
console.log(logCode, levelName);
};
/**
* @description Runtime errors, for example when parseInt or toFixed errors work wrong
* @function error
* @param message any type of message
* @param context Context will use to handle params. For example user IP address, username etc.
* @example
* loggerInstance.error('Error User', { username: 'John' })
*/
Logger.prototype.error = function (message, context) {
var logCode = LogTypes_1.default.ERROR;
var levelName = LogTypes_1.Levels.GET(logCode);
console.log(logCode, levelName);
};
/**
* @description Components unavailable or unexpected exceptions
* @function critical
* @param message any type of message
* @param context Context will use to handle params. For example user IP address, username etc.
* @example
* loggerInstance.critical('Critical message User', { username: 'John' })
*/
Logger.prototype.critical = function (message, context) {
var logCode = LogTypes_1.default.CRITICAL;
var levelName = LogTypes_1.Levels.GET(logCode);
console.log(logCode, levelName);
};
/**
* @description Action must be taken immediately.
* @function alert
* @param message any type of message
* @param context Context will use to handle params. For example user IP address, username etc.
* @example
* loggerInstance.alert('Alert message User', { username: 'John' })
*/
Logger.prototype.alert = function (message, context) {
var logCode = LogTypes_1.default.ALERT;
var levelName = LogTypes_1.Levels.GET(logCode);
console.log(logCode, levelName);
};
/**
* @description System is unusable
* @function emergency
* @param message any type of message
* @param context Context will use to handle params. For example user IP address, username etc.
* @example
* loggerInstance.emergency('Emergency message User', { username: 'John' })
*/
Logger.prototype.emergency = function (message, context) {
var logCode = LogTypes_1.default.EMERGENCY;
var levelName = LogTypes_1.Levels.GET(logCode);
console.log(logCode, levelName);
};
return Logger;
}());
exports.default = Logger;
{
"name": "nisanci",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@types/node": {
"version": "12.0.1",
"resolved": "https://registry.npmjs.org/@types/node/-/node-12.0.1.tgz",
"integrity": "sha512-7sy7DKVJrCTbaAERJZq/CU12bzdmpjRr321/Ne9QmzhB3iZ//L16Cizcni5hHNbANxDbxwMb9EFoWkM8KPkp0A=="
}
}
}
class ServerException extends Error {
constructor(message) {
super();
this.message = message
this.name = "ServerException"
}
}
export default ServerException
\ No newline at end of file
import ServerException from "./Exceptions/ServerException";
/**
* @class LogServer
* @description This class helps to debug messages send to the server.
*/
class LogServer {
/**
* Log Backend Settings
*/
private settings: any = {
useServer: false,
endPoint: '',
method: 'POST',
headers: {},
body: {}
}
/**
*
* @param serverSettings Log backend settings as a parameter
*/
constructor(serverSettings: any) {
this.settings = { ...serverSettings }
if (this.settings.useServer) {
if (!this.settings.endPoint) {
throw new ServerException("Endpoint must be defined!");
}
}
}
protected sendToLogServer(sendBody?: any) {
/**
* @description private function to send data to log backend
* @function sendToServer
* @param sendBody
*/
private sendToServer(sendBody?: any) {
const endPoint = this.settings.endPoint
const headers = this.settings.headers
const body = this.settings.body
const method = this.settings.method
fetch(endPoint, {
return fetch(endPoint, {
method: method,
headers: {
...headers
},
body: body
body: JSON.stringify({
...body,
...sendBody
})
})
}
}
\ No newline at end of file
/**
* @description public function to send data to log backend. If useServer flag is not true, data will not send to the server.
* @function sendToLogServer
* @param sendBody
*/
public sendToLogServer(sendBody?: any) {
if (this.settings.useServer) {
this.sendToServer(sendBody)
}
}
}
export default LogServer
\ No newline at end of file
import LogTypes, { Levels } from './LogTypes'
import LogServer from './LogServer'
/**
* @description Logger class. This class will have logging functions
......@@ -7,11 +8,30 @@ import LogTypes, { Levels } from './LogTypes'
class Logger {
private config: any = {
server: {}
useServer: false,
endPoint: '',
method: 'POST',
headers: {},
body: {}
}
private server: LogServer
constructor(logConfig?: any) {
this.config = logConfig
this.server = new LogServer(this.config.server)
}
/**
* @description Send extra fields to the log backend
* @function send
* @param sendBody Extra body field
* @example
* loggerInstance.send({ pageUrl: 'test', 'time': '12:30' })
*/
public send(sendBody?: any) {
return this.server.sendToLogServer(sendBody)
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment