ASC.WePlugins: added support CloudWatch

This commit is contained in:
Alexey Bannov 2022-11-03 15:09:24 +03:00
parent 2f2b1dcf12
commit 3a9fc421c8
5 changed files with 1300 additions and 33 deletions

View File

@ -3,5 +3,12 @@
"port": 5014,
"appsettings": "../../../../config",
"environment": "Development"
}
},
"aws":{
"accessKeyId": "",
"secretAccessKey": "",
"region": "",
"logGroupName": "/docspace/ASC.WebPlugins/instances/${instance-id}/general",
"logStreamName": "${guid} - ${date}"
}
}

View File

@ -13,18 +13,21 @@
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix"
},
"dependencies": {
"@aws-sdk/client-cloudwatch-logs": "^3.199.0",
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/typeorm": "^9.0.0",
"date-and-time": "^2.4.1",
"mysql2": "^2.3.3",
"nconf": "^0.12.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"typeorm": "^0.3.7",
"winston": "^3.8.1",
"winston-daily-rotate-file": "^4.7.1"
"winston": "^3.8.2",
"winston-cloudwatch": "^6.1.1",
"winston-daily-rotate-file": "^4.5.5"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",

View File

@ -1,4 +1,9 @@
import * as winston from "winston";
import * as WinstonCloudWatch from "winston-cloudwatch";
import * as date from "date-and-time";
import * as os from "os";
import * as config from "../config";
import { randomUUID } from "crypto";
import "winston-daily-rotate-file";
import * as path from "path";
import * as fs from "fs";
@ -21,6 +26,14 @@ if (!fs.existsSync(dirName)) {
fs.mkdirSync(dirName);
}
const aws = config.default.get("aws");
const accessKeyId = aws.accessKeyId;
const secretAccessKey = aws.secretAccessKey;
const awsRegion = aws.region;
const logGroupName = aws.logGroupName;
const logStreamName = aws.logStreamName;
const options = {
file: {
filename: fileName,
@ -38,20 +51,62 @@ const options = {
json: false,
colorize: true,
},
cloudWatch: {
name: 'aws',
level: "debug",
logGroupName: () => {
const hostname = os.hostname();
return logGroupName.replace("${instance-id}", hostname);
},
logStreamName: () => {
const now = new Date();
const guid = randomUUID();
const dateAsString = date.format(now, 'YYYY/MM/DDTHH.mm.ss');
return logStreamName.replace("${guid}", guid)
.replace("${date}", dateAsString);
},
awsRegion: awsRegion,
jsonMessage: true,
awsOptions: {
credentials: {
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey
}
}
}
};
const transports = [
const transports: winston.transport[] = [
new winston.transports.Console(options.console),
new winston.transports.DailyRotateFile(options.file),
new winston.transports.DailyRotateFile(options.file)
];
export default winston.createLogger({
if (aws != null && aws.accessKeyId !== '')
{
transports.push(new WinstonCloudWatch(options.cloudWatch));
}
const customFormat = winston.format(info => {
const now = new Date();
info.date = date.format(now, 'YYYY-MM-DD HH:mm:ss');
info.applicationContext = "ASC.WebPlugins";
info.level = info.level.toUpperCase();
const hostname = os.hostname();
info["instance-id"] = hostname;
return info;
})();
module.exports = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp({
format: "YYYY-MM-DD HH:mm:ss",
}),
winston.format.json()
customFormat,
winston.format.json()
),
transports: transports,
exitOnError: false,
});
});

View File

@ -11,7 +11,9 @@ async function bootstrap() {
try {
const app = await NestFactory.create(AppModule, { cors: false });
// app.enableCors();
await app.listen(port);
await app.listen(port, () => {
winston.info(`Start WebPlugins Service listening on port ${port} for http`);
});
} catch (e) {
winston.error(e);
}

File diff suppressed because it is too large Load Diff