Skip to content

Commit 641c99c

Browse files
committed
Initial commit
0 parents  commit 641c99c

19 files changed

+2559
-0
lines changed

.editorconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# http://editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
indent_style = space
8+
indent_size = 4
9+
end_of_line = lf
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true
12+
13+
[*.hbs]
14+
insert_final_newline = false
15+
16+
[*.json]
17+
indent_size = 2
18+
19+
[*.md]
20+
trim_trailing_whitespace = false
21+
22+
[*.{yml,yaml}]
23+
indent_size = 2
24+
25+
[Makefile]
26+
indent_style = tab

.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
plugins: ['ghost'],
3+
extends: [
4+
'plugin:ghost/node'
5+
]
6+
};

.github/workflows/test.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Test
2+
on:
3+
pull_request:
4+
push:
5+
branches:
6+
- master
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
node: [ 10, 12 ]
13+
env:
14+
FORCE_COLOR: 1
15+
name: Node ${{ matrix.node }}
16+
steps:
17+
- uses: actions/checkout@v1
18+
- uses: actions/setup-node@v1
19+
with:
20+
node-version: ${{ matrix.node }}
21+
22+
- run: yarn
23+
- run: yarn test

.gitignore

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Node template
2+
3+
# Logs
4+
logs
5+
*.log
6+
npm-debug.log*
7+
yarn-debug.log*
8+
yarn-error.log*
9+
10+
# Runtime data
11+
pids
12+
*.pid
13+
*.seed
14+
*.pid.lock
15+
16+
# Directory for instrumented libs generated by jscoverage/JSCover
17+
lib-cov
18+
19+
# Coverage directory used by tools like istanbul
20+
coverage
21+
22+
# nyc test coverage
23+
.nyc_output
24+
25+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
26+
.grunt
27+
28+
# Bower dependency directory (https://bower.io/)
29+
bower_components
30+
31+
# node-waf configuration
32+
.lock-wscript
33+
34+
# Compiled binary addons (https://nodejs.org/api/addons.html)
35+
build/Release
36+
37+
# Dependency directories
38+
node_modules/
39+
jspm_packages/
40+
41+
# Typescript v1 declaration files
42+
typings/
43+
44+
# Optional npm cache directory
45+
.npm
46+
47+
# Optional eslint cache
48+
.eslintcache
49+
50+
# Optional REPL history
51+
.node_repl_history
52+
53+
# Output of 'npm pack'
54+
*.tgz
55+
56+
# Yarn Integrity file
57+
.yarn-integrity
58+
59+
# dotenv environment variables file
60+
.env
61+
62+
# IDE
63+
.idea/*
64+
*.iml
65+
*.sublime-*
66+
.vscode/*
67+
68+
# OSX
69+
.DS_Store
70+
71+
# Elasticsearch Bunyan Custom

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Elasticsearch Bunyan
2+
3+
## Install
4+
5+
`npm install elasticsearch-bunyan --save`
6+
7+
or
8+
9+
`yarn add elasticsearch-bunyan`
10+
11+
12+
## Usage
13+
14+
15+
## Develop
16+
17+
1. `git clone` this repo & `cd` into it as usual
18+
2. Run `yarn` to install top-level dependencies.
19+
20+
21+
## Run
22+
23+
- `yarn dev`
24+
25+
26+
## Test
27+
28+
- `yarn lint` run just eslint
29+
- `yarn test` run lint and tests
30+
31+
32+
## Publish
33+
34+
- `yarn ship`
35+
36+
37+
# Copyright & License
38+
39+
Copyright (c) 2021 Ghost Foundation. All rights reserved.
40+
41+
This code is considered closed-source and not for distribution. There is no opensource license associated with this project.

index.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const WriteQueue = require('./lib/writeQueue');
2+
const PeriodTrigger = require('./lib/periodTrigger');
3+
const ThresholdTrigger = require('./lib/thresholdTrigger');
4+
const FileRotator = require('./lib/fileRotator');
5+
const {BytesWritten, FileStartTime, Rotate, NewFile} = require('./lib/customEvents');
6+
7+
const fileStreams = [];
8+
9+
class RotatingFileStream {
10+
constructor(config) {
11+
if (typeof config.path !== 'string') {
12+
throw new Error('Must provide a string for path');
13+
}
14+
if (fileStreams.indexOf(config.path) >= 0) {
15+
throw new Error('Rotating log already exists for path: ', config.path);
16+
}
17+
fileStreams.push(config.path);
18+
this._path = config.path;
19+
this._rotator = new FileRotator(config);
20+
this._queue = new WriteQueue();
21+
this._triggers = [];
22+
if (config.period) {
23+
const periodTrigger = new PeriodTrigger(config.period);
24+
this.triggers.push(periodTrigger);
25+
if (config.rotateExisting) {
26+
this._rotator.once(FileStartTime, (startTime) => {
27+
periodTrigger.setInitialTime(startTime);
28+
});
29+
}
30+
}
31+
if (config.threshold) {
32+
const thresholdTrigger = new ThresholdTrigger(config.threshold);
33+
this._queue.on(BytesWritten, bytes => thresholdTrigger.updateWritten(bytes));
34+
this._triggers.push(thresholdTrigger);
35+
}
36+
this._rotatingLock = false;
37+
this._triggers.forEach((trigger) => {
38+
trigger.on(Rotate, this._rotate);
39+
});
40+
this._rotator.on(NewFile, () => {
41+
this._queue.setFileHandle(this._rotator.getCurrentHandle());
42+
this._triggers.forEach(trigger => trigger.newFile());
43+
});
44+
this._initialised = this._rotator.initialise();
45+
}
46+
47+
async _rotate() {
48+
if (this._rotatingLock) {
49+
// Already rotating
50+
return;
51+
}
52+
this._rotatingLock = true;
53+
await this._queue.pause();
54+
const nextFileHandle = await this._rotator.rotate();
55+
this._triggers.forEach(trigger => trigger.rotated());
56+
this._queue.setFileHandle(nextFileHandle);
57+
this._rotatingLock = false;
58+
}
59+
60+
async write(data) {
61+
this._queue.push(data);
62+
}
63+
64+
async end() {
65+
await this._initialised;
66+
await this._queue.shutdown();
67+
await this._rotator.shutdown();
68+
this._triggers.forEach(trigger => trigger.shutdown());
69+
const fileStreamIndex = fileStreams.indexOf(this._path);
70+
if (fileStreamIndex >= 0) {
71+
fileStreams.splice(fileStreamIndex, 1);
72+
}
73+
}
74+
75+
destroy() {
76+
this.end();
77+
}
78+
79+
destroySoon() {
80+
this.end();
81+
}
82+
}
83+
84+
module.exports = RotatingFileStream;

lib/customEvents.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
// Emitted when a new rotation is needed
3+
Rotate: Symbol('rotate'),
4+
// Emitted when a new file descriptor is in use, after a rotation
5+
NewFile: Symbol('newFile'),
6+
// Emitted once with the 'created at' of the initial file
7+
FileStartTime: Symbol('fileStartTime'),
8+
// Emitted whenever data is written to the file with the number of bytes
9+
BytesWritten: Symbol('bytesWritten')
10+
};

0 commit comments

Comments
 (0)