Commit dfdb6f5d authored by rockyl's avatar rockyl

日志文件修改

parent 5cdbd1fd
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var restify = _interopDefault(require('restify')); var restify = _interopDefault(require('restify'));
var imagemin = _interopDefault(require('imagemin'));
var imageminJpegtran = _interopDefault(require('imagemin-jpegtran'));
var imageminPngquant = _interopDefault(require('imagemin-pngquant'));
var Eurekaclient = _interopDefault(require('eureka-client-ts')); var Eurekaclient = _interopDefault(require('eureka-client-ts'));
var log4js = require('log4js'); var log4js = require('log4js');
var path = _interopDefault(require('path')); var path = _interopDefault(require('path'));
var fs = _interopDefault(require('fs-extra')); var fs = _interopDefault(require('fs-extra'));
var imagemin = _interopDefault(require('imagemin'));
var imageminJpegtran = _interopDefault(require('imagemin-jpegtran'));
var imageminPngquant = _interopDefault(require('imagemin-pngquant'));
/** /**
* Created by rockyl on 2020-02-13. * Created by rockyl on 2020-02-13.
...@@ -21,11 +21,14 @@ fs.ensureDir(logPath); ...@@ -21,11 +21,14 @@ fs.ensureDir(logPath);
log4js.configure({ log4js.configure({
appenders: { appenders: {
out: { type: 'console' }, out: {type: 'console'},
allLog: { type: 'file', filename: path.join(logPath, 'log.log'), keepFileExt: true, maxLogSize: 10485760, backups: 3 }, allLog: {
type: 'dateFile',
filename: path.join(logPath, 'log.log'),
},
}, },
categories: { categories: {
default: { appenders: ['out', 'allLog'], level: 'debug' }, default: {appenders: ['out', 'allLog'], level: 'debug'},
} }
}); });
...@@ -50,6 +53,61 @@ async function getEurekaClient(port) { ...@@ -50,6 +53,61 @@ async function getEurekaClient(port) {
return eureka; return eureka;
} }
/**
* Created by rockyl on 2020-02-13.
*/
const acceptTypes = ['image/png', 'image/jpeg'];
function applyApi(server){
server.get('/echo/:name', function (req, res, next) {
res.send(req.params);
return next();
});
server.get('/monitor/check', function (req, res, next) {
res.sendRaw('ok');
return next();
});
server.post('/tinify', async function (req, res, next) {
logger.info('/tinify');
let file = req.files.file;
if(acceptTypes.includes(file.type)){
try {
const buffer = await imagemin([file.path], {
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: [0.6, 0.8]
})
]
});
res.sendRaw(buffer[0].data, {
'Content-Type': file.type,
});
}catch (e) {
logger.error('/tinify', 'compress failed');
res.send({
code: 2,
msg: 'compress failed'
});
}
}else{
logger.error('/tinify', 'type error');
res.send({
code: 1,
msg: 'type error'
});
}
return next();
});
}
/** /**
* Created by rockyl on 2020-02-12. * Created by rockyl on 2020-02-12.
*/ */
...@@ -58,8 +116,6 @@ let [_, __, port = '8090'] = process.argv; ...@@ -58,8 +116,6 @@ let [_, __, port = '8090'] = process.argv;
let eureka; let eureka;
port = parseInt(port); port = parseInt(port);
const acceptTypes = ['image/png', 'image/jpeg'];
const server = restify.createServer({ const server = restify.createServer({
name: 'tiny-image', name: 'tiny-image',
version: '1.0.0' version: '1.0.0'
...@@ -69,47 +125,7 @@ server.use(restify.plugins.acceptParser(server.acceptable)); ...@@ -69,47 +125,7 @@ server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser()); server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser()); server.use(restify.plugins.bodyParser());
server.get('/echo/:name', function (req, res, next) { applyApi(server);
res.send(req.params);
return next();
});
server.post('/tinify', async function (req, res, next) {
logger.info('/tinify');
let file = req.files.file;
if(acceptTypes.includes(file.type)){
try {
const buffer = await imagemin([file.path], {
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: [0.6, 0.8]
})
]
});
res.sendRaw(buffer[0].data, {
'Content-Type': file.type,
});
}catch (e) {
logger.error('/tinify', 'compress failed');
res.send({
code: 2,
msg: 'compress failed'
});
}
}else{
logger.error('/tinify', 'type error');
res.send({
code: 1,
msg: 'type error'
});
}
return next();
});
process.on('uncaughtException', (err) => { process.on('uncaughtException', (err) => {
logger.error(err); logger.error(err);
......
{"version":3,"file":"index.js","sources":["../src/logger.js","../src/eureka.js","../src/index.js"],"sourcesContent":["/**\n * Created by rockyl on 2020-02-13.\n */\n\nimport { configure, getLogger } from 'log4js';\nimport path from \"path\";\nimport fs from \"fs-extra\";\n\nconst logPath = path.join(process.env.HOME || process.env.USERPROFILE, 'logs', 'tiny-image');\n\nfs.ensureDir(logPath);\n\nconfigure({\n\tappenders: {\n\t\tout: { type: 'console' },\n\t\tallLog: { type: 'file', filename: path.join(logPath, 'log.log'), keepFileExt: true, maxLogSize: 10485760, backups: 3 },\n\t},\n\tcategories: {\n\t\tdefault: { appenders: ['out', 'allLog'], level: 'debug' },\n\t}\n\n});\n\nconst logger = getLogger();\n\nexport default logger;","import Eurekaclient from 'eureka-client-ts';\nimport logger from './logger';\n\nconst eurekaConfig = {\n appName: 'tiny-image',\n services: ['tiny-image'],\n};\n\nexport async function getEurekaClient(port) {\n eurekaConfig.port = port;\n let eureka;\n try {\n eureka = new Eurekaclient(eurekaConfig);\n await eureka.start();\n } catch (e) {\n logger.error(e);\n }\n\n return eureka;\n}\n","/**\n * Created by rockyl on 2020-02-12.\n */\n\nimport restify from 'restify';\nimport imagemin from 'imagemin';\nimport imageminJpegtran from 'imagemin-jpegtran';\nimport imageminPngquant from 'imagemin-pngquant';\nimport {getEurekaClient} from \"./eureka\";\nimport logger from \"./logger\";\n\nlet [_, __, port = '8090'] = process.argv;\nlet eureka;\nport = parseInt(port);\n\nconst acceptTypes = ['image/png', 'image/jpeg'];\n\nconst server = restify.createServer({\n\tname: 'tiny-image',\n\tversion: '1.0.0'\n});\n\nserver.use(restify.plugins.acceptParser(server.acceptable));\nserver.use(restify.plugins.queryParser());\nserver.use(restify.plugins.bodyParser());\n\nserver.get('/echo/:name', function (req, res, next) {\n\tres.send(req.params);\n\treturn next();\n});\n\nserver.post('/tinify', async function (req, res, next) {\n\tlogger.info('/tinify');\n\n\tlet file = req.files.file;\n\n\tif(acceptTypes.includes(file.type)){\n\t\ttry {\n\t\t\tconst buffer = await imagemin([file.path], {\n\t\t\t\tplugins: [\n\t\t\t\t\timageminJpegtran(),\n\t\t\t\t\timageminPngquant({\n\t\t\t\t\t\tquality: [0.6, 0.8]\n\t\t\t\t\t})\n\t\t\t\t]\n\t\t\t});\n\n\t\t\tres.sendRaw(buffer[0].data, {\n\t\t\t\t'Content-Type': file.type,\n\t\t\t});\n\t\t}catch (e) {\n\t\t\tlogger.error('/tinify', 'compress failed');\n\t\t\tres.send({\n\t\t\t\tcode: 2,\n\t\t\t\tmsg: 'compress failed'\n\t\t\t});\n\t\t}\n\t}else{\n\t\tlogger.error('/tinify', 'type error');\n\t\tres.send({\n\t\t\tcode: 1,\n\t\t\tmsg: 'type error'\n\t\t});\n\t}\n\n\treturn next();\n});\n\nprocess.on('uncaughtException', (err) => {\n\tlogger.error(err);\n});\n\nprocess.on(\"SIGTERM\", () => {\n\teureka.stop();\n\tsetTimeout(() => {\n\t\tprocess.exit(1)\n\t}, 6000);\n});\n\nserver.listen(port, async function () {\n\ttry {\n\t\teureka = await getEurekaClient(port);\n\t\tlogger.info('eureka register success');\n\t} catch (e) {\n\t\tlogger.error(e.name);\n\t}\n\tlogger.info('%s listening at %s', server.name, server.url);\n});\n"],"names":["configure","getLogger"],"mappings":";;;;;;;;;;;;;AAAA;;;AAGA,AAIA;AACA,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC;;AAE7F,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;;AAEtBA,gBAAS,CAAC;CACT,SAAS,EAAE;EACV,GAAG,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;EACxB,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,SAAS,CA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C;GACP,GAAG,EAAE,YAAY;GACjB,CAAC,CAAC;EACH;;CAED,OAAO,IAAI,EAAE,CAAC;CACd,CAAC,CAAC;;AAEH,OAAO,CAAC,EAAE,CAAC,mBAAmB,EAAE,CAAC,GAAG,KAAK;CACxC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;CAClB,CAAC,CAAC;;AAEH,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM;CAC3B,MAAM,CAAC,IAAI,EAAE,CAAC;CACd,UAAU,CAAC,MAAM;EAChB,OAAO,CAAC,IAAI,CAAC,CAAC,EAAC;EACf,EAAE,IAAI,CAAC,CAAC;CACT,CAAC,CAAC;;AAEH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,kBAAkB;CACrC,IAAI;EACH,MAAM,GAAG,MAAM,eAAe,CAAC,IAAI,CAAC,CAAC;EACrC,MAAM,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;EACvC,CAAC,OAAO,CAAC,EAAE;EACX,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;EACrB;CACD,MAAM,CAAC,IAAI,CAAC,oBAAoB,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;CAC3D,CAAC,CAAC"} {"version":3,"file":"index.js","sources":["../src/logger.js","../src/eureka.js","../src/api.js","../src/index.js"],"sourcesContent":["/**\n * Created by rockyl on 2020-02-13.\n */\n\nimport {configure, getLogger} from 'log4js';\nimport path from \"path\";\nimport fs from \"fs-extra\";\n\nconst logPath = path.join(process.env.HOME || process.env.USERPROFILE, 'logs', 'tiny-image');\n\nfs.ensureDir(logPath);\n\nconfigure({\n\tappenders: {\n\t\tout: {type: 'console'},\n\t\tallLog: {\n\t\t\ttype: 'dateFile',\n\t\t\tfilename: path.join(logPath, 'log.log'),\n\t\t},\n\t},\n\tcategories: {\n\t\tdefault: {appenders: ['out', 'allLog'], level: 'debug'},\n\t}\n\n});\n\nconst logger = getLogger();\n\nexport default logger;","import Eurekaclient from 'eureka-client-ts';\nimport logger from './logger';\n\nconst eurekaConfig = {\n appName: 'tiny-image',\n services: ['tiny-image'],\n};\n\nexport async function getEurekaClient(port) {\n eurekaConfig.port = port;\n let eureka;\n try {\n eureka = new Eurekaclient(eurekaConfig);\n await eureka.start();\n } catch (e) {\n logger.error(e);\n }\n\n return eureka;\n}\n","/**\n * Created by rockyl on 2020-02-13.\n */\n\nimport logger from \"./logger\";\nimport imagemin from \"imagemin\";\nimport imageminJpegtran from \"imagemin-jpegtran\";\nimport imageminPngquant from \"imagemin-pngquant\";\n\nconst acceptTypes = ['image/png', 'image/jpeg'];\n\nexport default function applyApi(server){\n\tserver.get('/echo/:name', function (req, res, next) {\n\t\tres.send(req.params);\n\t\treturn next();\n\t});\n\n\tserver.get('/monitor/check', function (req, res, next) {\n\t\tres.sendRaw('ok');\n\t\treturn next();\n\t});\n\n\tserver.post('/tinify', async function (req, res, next) {\n\t\tlogger.info('/tinify');\n\n\t\tlet file = req.files.file;\n\n\t\tif(acceptTypes.includes(file.type)){\n\t\t\ttry {\n\t\t\t\tconst buffer = await imagemin([file.path], {\n\t\t\t\t\tplugins: [\n\t\t\t\t\t\timageminJpegtran(),\n\t\t\t\t\t\timageminPngquant({\n\t\t\t\t\t\t\tquality: [0.6, 0.8]\n\t\t\t\t\t\t})\n\t\t\t\t\t]\n\t\t\t\t});\n\n\t\t\t\tres.sendRaw(buffer[0].data, {\n\t\t\t\t\t'Content-Type': file.type,\n\t\t\t\t});\n\t\t\t}catch (e) {\n\t\t\t\tlogger.error('/tinify', 'compress failed');\n\t\t\t\tres.send({\n\t\t\t\t\tcode: 2,\n\t\t\t\t\tmsg: 'compress failed'\n\t\t\t\t});\n\t\t\t}\n\t\t}else{\n\t\t\tlogger.error('/tinify', 'type error');\n\t\t\tres.send({\n\t\t\t\tcode: 1,\n\t\t\t\tmsg: 'type error'\n\t\t\t});\n\t\t}\n\n\t\treturn next();\n\t});\n}","/**\n * Created by rockyl on 2020-02-12.\n */\n\nimport restify from 'restify';\nimport {getEurekaClient} from \"./eureka\";\nimport logger from \"./logger\";\nimport applyApi from \"./api\";\n\nlet [_, __, port = '8090'] = process.argv;\nlet eureka;\nport = parseInt(port);\n\nconst server = restify.createServer({\n\tname: 'tiny-image',\n\tversion: '1.0.0'\n});\n\nserver.use(restify.plugins.acceptParser(server.acceptable));\nserver.use(restify.plugins.queryParser());\nserver.use(restify.plugins.bodyParser());\n\napplyApi(server);\n\nprocess.on('uncaughtException', (err) => {\n\tlogger.error(err);\n});\n\nprocess.on(\"SIGTERM\", () => {\n\teureka.stop();\n\tsetTimeout(() => {\n\t\tprocess.exit(1)\n\t}, 6000);\n});\n\nserver.listen(port, async function () {\n\ttry {\n\t\teureka = await getEurekaClient(port);\n\t\tlogger.info('eureka register success');\n\t} catch (e) {\n\t\tlogger.error(e.name);\n\t}\n\tlogger.info('%s listening at %s', server.name, server.url);\n});\n"],"names":["configure","getLogger"],"mappings":";;;;;;;;;;;;;AAAA;;;AAGA,AAIA;AACA,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC;;AAE7F,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;;AAEtBA,gBAAS,CAAC;CACT,SAAS,EAAE;EACV,GAAG,EAAE,CAAC,IAAI,EAAE,SAAS,CAAC;EACtB,MAAM,EAAE;GACP,IAAI,EAAE,UAAU;GAChB,QAAQ,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC;GACvC;EACD;CACD,UAAU,EAAE;EACX,OAAO,EAAE,CAAC,SAAS,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC;EACvD;;CAED,CAAC,CAAC;;AAEH,MAAM,MAAM,GAAGC,gBAAS,EAA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AAC1C,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;;AAEzC,QAAQ,CAAC,MAAM,CAAC,CAAC;;AAEjB,OAAO,CAAC,EAAE,CAAC,mBAAmB,EAAE,CAAC,GAAG,KAAK;CACxC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;CAClB,CAAC,CAAC;;AAEH,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM;CAC3B,MAAM,CAAC,IAAI,EAAE,CAAC;CACd,UAAU,CAAC,MAAM;EAChB,OAAO,CAAC,IAAI,CAAC,CAAC,EAAC;EACf,EAAE,IAAI,CAAC,CAAC;CACT,CAAC,CAAC;;AAEH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,kBAAkB;CACrC,IAAI;EACH,MAAM,GAAG,MAAM,eAAe,CAAC,IAAI,CAAC,CAAC;EACrC,MAAM,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;EACvC,CAAC,OAAO,CAAC,EAAE;EACX,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;EACrB;CACD,MAAM,CAAC,IAAI,CAAC,oBAAoB,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;CAC3D,CAAC,CAAC"}
\ No newline at end of file \ No newline at end of file
/**
* Created by rockyl on 2020-02-13.
*/
import logger from "./logger";
import imagemin from "imagemin";
import imageminJpegtran from "imagemin-jpegtran";
import imageminPngquant from "imagemin-pngquant";
const acceptTypes = ['image/png', 'image/jpeg'];
export default function applyApi(server){
server.get('/echo/:name', function (req, res, next) {
res.send(req.params);
return next();
});
server.get('/monitor/check', function (req, res, next) {
res.sendRaw('ok');
return next();
});
server.post('/tinify', async function (req, res, next) {
logger.info('/tinify');
let file = req.files.file;
if(acceptTypes.includes(file.type)){
try {
const buffer = await imagemin([file.path], {
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: [0.6, 0.8]
})
]
});
res.sendRaw(buffer[0].data, {
'Content-Type': file.type,
});
}catch (e) {
logger.error('/tinify', 'compress failed');
res.send({
code: 2,
msg: 'compress failed'
});
}
}else{
logger.error('/tinify', 'type error');
res.send({
code: 1,
msg: 'type error'
});
}
return next();
});
}
\ No newline at end of file
...@@ -3,18 +3,14 @@ ...@@ -3,18 +3,14 @@
*/ */
import restify from 'restify'; import restify from 'restify';
import imagemin from 'imagemin';
import imageminJpegtran from 'imagemin-jpegtran';
import imageminPngquant from 'imagemin-pngquant';
import {getEurekaClient} from "./eureka"; import {getEurekaClient} from "./eureka";
import logger from "./logger"; import logger from "./logger";
import applyApi from "./api";
let [_, __, port = '8090'] = process.argv; let [_, __, port = '8090'] = process.argv;
let eureka; let eureka;
port = parseInt(port); port = parseInt(port);
const acceptTypes = ['image/png', 'image/jpeg'];
const server = restify.createServer({ const server = restify.createServer({
name: 'tiny-image', name: 'tiny-image',
version: '1.0.0' version: '1.0.0'
...@@ -24,47 +20,7 @@ server.use(restify.plugins.acceptParser(server.acceptable)); ...@@ -24,47 +20,7 @@ server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser()); server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser()); server.use(restify.plugins.bodyParser());
server.get('/echo/:name', function (req, res, next) { applyApi(server);
res.send(req.params);
return next();
});
server.post('/tinify', async function (req, res, next) {
logger.info('/tinify');
let file = req.files.file;
if(acceptTypes.includes(file.type)){
try {
const buffer = await imagemin([file.path], {
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: [0.6, 0.8]
})
]
});
res.sendRaw(buffer[0].data, {
'Content-Type': file.type,
});
}catch (e) {
logger.error('/tinify', 'compress failed');
res.send({
code: 2,
msg: 'compress failed'
});
}
}else{
logger.error('/tinify', 'type error');
res.send({
code: 1,
msg: 'type error'
});
}
return next();
});
process.on('uncaughtException', (err) => { process.on('uncaughtException', (err) => {
logger.error(err); logger.error(err);
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* Created by rockyl on 2020-02-13. * Created by rockyl on 2020-02-13.
*/ */
import { configure, getLogger } from 'log4js'; import {configure, getLogger} from 'log4js';
import path from "path"; import path from "path";
import fs from "fs-extra"; import fs from "fs-extra";
...@@ -12,11 +12,14 @@ fs.ensureDir(logPath); ...@@ -12,11 +12,14 @@ fs.ensureDir(logPath);
configure({ configure({
appenders: { appenders: {
out: { type: 'console' }, out: {type: 'console'},
allLog: { type: 'file', filename: path.join(logPath, 'log.log'), keepFileExt: true, maxLogSize: 10485760, backups: 3 }, allLog: {
type: 'dateFile',
filename: path.join(logPath, 'log.log'),
},
}, },
categories: { categories: {
default: { appenders: ['out', 'allLog'], level: 'debug' }, default: {appenders: ['out', 'allLog'], level: 'debug'},
} }
}); });
......
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