added logger
This commit is contained in:
5
src/abc/xyz.ts
Normal file
5
src/abc/xyz.ts
Normal file
@ -0,0 +1,5 @@
|
||||
class InfrastructureAutoLoadable {
|
||||
public load (): void {console.log("TEST");}
|
||||
}
|
||||
|
||||
export default new InfrastructureAutoLoadable();
|
||||
40
src/framework/logger/logger.ts
Normal file
40
src/framework/logger/logger.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import winston, { format, transports, transport as Transport, Logger } from "winston";
|
||||
import { Format } from "logform";
|
||||
|
||||
const { combine, timestamp, label, printf, errors, splat } = format;
|
||||
|
||||
// Custom logging format
|
||||
const customFormat = printf(({ level, message, label, timestamp, stack }) => {
|
||||
return `${timestamp} [${label || "-"}] ${level}: ${message} ${stack || ""}`;
|
||||
});
|
||||
|
||||
// Custom combined logging format:
|
||||
const customCombinedFormat = (module: string): Format =>
|
||||
combine(
|
||||
errors({ stack: true }),
|
||||
format.colorize({ level: true }),
|
||||
label({ label: module }),
|
||||
timestamp(),
|
||||
splat(),
|
||||
customFormat
|
||||
);
|
||||
|
||||
// Custom transports:
|
||||
const customTransports = (): Transport[] => [new transports.Console()];
|
||||
|
||||
// Container to provide different pre-configured loggers
|
||||
const logContainer = new winston.Container();
|
||||
|
||||
// Default logger for modules:
|
||||
const getLogger = (module: string): Logger => {
|
||||
if (!logContainer.has(module)) {
|
||||
logContainer.add(module, {
|
||||
format: customCombinedFormat(module),
|
||||
transports: customTransports()
|
||||
});
|
||||
}
|
||||
return logContainer.get(module);
|
||||
};
|
||||
|
||||
export default getLogger;
|
||||
export const defaultLogger = getLogger("default");
|
||||
65
src/myloader.mjs
Normal file
65
src/myloader.mjs
Normal file
@ -0,0 +1,65 @@
|
||||
import tsconfig from '../tsconfig.json';
|
||||
import * as path from 'path';
|
||||
import { fileURLToPath } from 'url'
|
||||
import { createRequire } from 'module'
|
||||
const require = createRequire(fileURLToPath(import.meta.url))
|
||||
|
||||
const baseURL = "file:///" + path.resolve(path.dirname(''), tsconfig.compilerOptions.baseUrl);
|
||||
|
||||
const esm = require('../node_modules/ts-node/dist/esm')
|
||||
|
||||
const hooks = esm.registerAndCreateEsmHooks()
|
||||
|
||||
function doesAliasMatch(path, specifier) {
|
||||
if (path.alias.endsWith("*") && path.replacement.endsWith("*")) {
|
||||
return specifier.startsWith(path.alias.slice(0, -1));
|
||||
} else if (!path.alias.endsWith("*") && !path.replacement.endsWith("*")) {
|
||||
return (path.alias === specifier);
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function findAlias(specifier) {
|
||||
const paths = tsconfig.compilerOptions.paths;
|
||||
let found = undefined;
|
||||
Object.keys(paths).forEach(function (path) {
|
||||
let temp = { alias: path, replacement: paths[path][0] };
|
||||
if (doesAliasMatch(temp, specifier)) {
|
||||
found = temp;
|
||||
}
|
||||
});
|
||||
return found;
|
||||
}
|
||||
|
||||
function aliasSpecifier(alias, specifier) {
|
||||
if (alias.alias.endsWith("*")) {
|
||||
return specifier.replace(alias.alias.slice(0, -1), alias.replacement.slice(0, -1));
|
||||
} else {
|
||||
return specifier.replace(alias.alias, alias.replacement);
|
||||
}
|
||||
}
|
||||
|
||||
export function load(url, context, defaultLoad) {
|
||||
return hooks.load(url, context, defaultLoad)
|
||||
}
|
||||
|
||||
export function resolve(specifier, context, defaultResolve) {
|
||||
const { parentURL = baseURL + "/server.ts" } = context;
|
||||
const alias = findAlias(specifier);
|
||||
if (alias !== undefined) {
|
||||
const tspecifier = specifier;
|
||||
specifier = aliasSpecifier(alias, specifier);
|
||||
let relativpath = path.relative(path.dirname(parentURL), baseURL);
|
||||
if (relativpath === "") relativpath = ".";
|
||||
specifier = "./" + relativpath + "/" + specifier.replace("./", "");
|
||||
}
|
||||
return hooks.resolve(specifier, context, defaultResolve)
|
||||
}
|
||||
|
||||
export function getFormat(url, context, defaultGetFormat) {
|
||||
return hooks.getFormat(url, context, defaultGetFormat);
|
||||
}
|
||||
|
||||
export function transformSource(source, context, defaultTransformSource) {
|
||||
return hooks.transformSource(source, context, defaultTransformSource);
|
||||
}
|
||||
63
src/server.ts
Normal file
63
src/server.ts
Normal file
@ -0,0 +1,63 @@
|
||||
import Fastify from "fastify";
|
||||
import fastifySensible from "fastify-sensible";
|
||||
import GracefulServer from "@gquittet/graceful-server";
|
||||
import getlogger, {defaultLogger} from "@logger";
|
||||
|
||||
const fastify = Fastify({
|
||||
logger: true
|
||||
});
|
||||
fastify.register(fastifySensible);
|
||||
|
||||
const gracefulServer = GracefulServer(fastify.server);
|
||||
|
||||
gracefulServer.on(GracefulServer.READY, () => {
|
||||
console.log("Server is ready");
|
||||
});
|
||||
|
||||
gracefulServer.on(GracefulServer.SHUTTING_DOWN, () => {
|
||||
console.log("Server is shutting down");
|
||||
});
|
||||
|
||||
gracefulServer.on(GracefulServer.SHUTDOWN, error => {
|
||||
console.log("Server is down because of", error.message);
|
||||
process.exit();
|
||||
});
|
||||
|
||||
// Declare a route
|
||||
fastify.get("/", function (request, reply) {
|
||||
reply.send({ hello: "world" });
|
||||
});
|
||||
|
||||
// Run the server!
|
||||
const start = async () => {
|
||||
try {
|
||||
await fastify.listen(3000);
|
||||
gracefulServer.setReady();
|
||||
} catch (err) {
|
||||
fastify.log.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
start();
|
||||
|
||||
const c = function (): void {
|
||||
const v = function (): void {
|
||||
throw new Error("Oh no!");
|
||||
};
|
||||
return v();
|
||||
};
|
||||
|
||||
const abc = getlogger("abc");
|
||||
abc.info("Ich bin ein Test!");
|
||||
abc.warn("Ich bin ein Test!");
|
||||
try {
|
||||
c();
|
||||
}
|
||||
catch (e) {
|
||||
abc.warn(e);
|
||||
}
|
||||
const xyz = getlogger("xyz");
|
||||
xyz.error("Ich bin ein Test!");
|
||||
xyz.error("Ich bin ein '%s' Test!", "test");
|
||||
|
||||
defaultLogger.error("Ich bin ein Test!");
|
||||
Reference in New Issue
Block a user