Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 60bfac8

Browse files
How to log from sandboxes
1 parent 499d764 commit 60bfac8

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

‎JavaScript/3-sandbox/application.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
console.log('Write logs with console.log');
4+
console.dir({ example: [0, 1, 2, 3] });
5+
console.error('Error message');

‎JavaScript/3-sandbox/log/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!.gitignore

‎JavaScript/3-sandbox/main.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const vm = require('vm');
4+
const fs = require('fs');
5+
const util = require('util');
6+
const metalog = require('metalog');
7+
8+
const log = metalog({
9+
path: './log',
10+
node: 'S1N1',
11+
writeInterval: 5000,
12+
writeBuffer: 64 * 1024,
13+
keepDays: 5,
14+
toStdout: [],
15+
}).bind('app1');
16+
17+
const logInfo = (...args) => log.info(util.format(...args));
18+
const logError = (...args) => log.error(util.format(...args));
19+
20+
const context = {
21+
module: {},
22+
console: {
23+
log: logInfo,
24+
dir: logInfo,
25+
debug: logInfo,
26+
error: logError,
27+
},
28+
};
29+
30+
context.global = context;
31+
const sandbox = vm.createContext(context);
32+
33+
const fileName = './application.js';
34+
fs.readFile(fileName, 'utf8', (err, src) => {
35+
const script = new vm.Script(`module.exports = () => {\n${src}\n};`);
36+
const execute = script.runInNewContext(sandbox);
37+
execute();
38+
log.logger.close();
39+
});

‎JavaScript/3-sandbox/package-lock.json‎

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎JavaScript/3-sandbox/package.json‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "metalog-example",
3+
"version": "1.0.0",
4+
"requires": true,
5+
"engines": {
6+
"node": ">=6.0.0"
7+
},
8+
"main": "./main.js",
9+
"dependencies": {
10+
"@metarhia/common": "^1.5.0",
11+
"concolor": "^0.1.12",
12+
"metalog": "^1.1.0",
13+
"metastreams": "^0.1.1"
14+
}
15+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /