You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
865 B
37 lines
865 B
6 months ago
|
'use strict'
|
||
|
|
||
|
/**
|
||
|
* Save log to file for Loki
|
||
|
**/
|
||
|
|
||
|
const express = require('express');
|
||
|
const { resolve } = require('node:path');
|
||
|
const {default: common} = require('./common.js');
|
||
|
|
||
|
|
||
|
const router = express.Router();
|
||
|
|
||
|
|
||
|
//写入文件接口
|
||
|
//@group - 分组,log文件名
|
||
|
//其它所有参数均会写入log
|
||
|
router.post('/save', (req, res) => {
|
||
|
let out = {code: 1};
|
||
|
|
||
|
const group = req.body.group ? req.body.group : 'test';
|
||
|
|
||
|
const logDir = '/var/log/loki/'; //日志保存目录
|
||
|
const logFile = resolve(logDir, `${group}.log`);
|
||
|
|
||
|
const ignoreKeys = ['group', 'timestamp'];
|
||
|
let params = common.copyBodyParams(req.body, ignoreKeys);
|
||
|
params.timestamp = common.getTimestampInSeconds();
|
||
|
|
||
|
const json = JSON.stringify(params);
|
||
|
common.saveLog(logFile, `${json}\n`);
|
||
|
|
||
|
return res.status(200).json(out);
|
||
|
});
|
||
|
|
||
|
|
||
|
exports.default = router;
|