Skip to content

Commit 7e1a29a

Browse files
committed
feat(log): ghost log command
closes #42 - read bunyan log outputted by ghost - deps: [email protected] - deps: [email protected] (used for the PrettyStream)
1 parent f827ea4 commit 7e1a29a

File tree

4 files changed

+108
-6
lines changed

4 files changed

+108
-6
lines changed

lib/commands.js

+17
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ module.exports = {
5151
}].concat(advancedOptions)
5252
},
5353

54+
log: {
55+
description: 'view logs of a running ghost process',
56+
57+
arguments: ['name'],
58+
options: [{
59+
name: 'number',
60+
alias: 'n',
61+
description: 'Number of lines to view',
62+
defaultValue: 20
63+
}, {
64+
name: 'follow',
65+
alias: 'f',
66+
description: 'Follow the log file (similar to `tail -f`)',
67+
flag: true
68+
}]
69+
},
70+
5471
ls: {
5572
description: 'view running ghost processes'
5673
},

lib/commands/log.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
const path = require('path');
3+
const includes = require('lodash/includes');
4+
const sliceFile = require('slice-file');
5+
const PrettyStream = require('ghost-ignition/lib/logging/PrettyStream');
6+
7+
const errors = require('../errors');
8+
const Config = require('../utils/config');
9+
10+
module.exports.execute = function execute(name, options) {
11+
let systemConfig = Config.load('system');
12+
let instance = systemConfig.get(`instances.${name}`);
13+
14+
if (!instance) {
15+
throw new errors.SystemError(`Ghost instance '${name}' does not exist`);
16+
}
17+
18+
// Change into the cwd of the running ghost instance so we can do things
19+
// relative to that
20+
process.chdir(instance.cwd);
21+
22+
let instanceConfig = Config.load(instance.mode);
23+
24+
// Check if logging file transport is set in config
25+
if (!includes(instanceConfig.get('logging.transports', []), 'file')) {
26+
// TODO: fallback to process manager log retrieval?
27+
throw new errors.ConfigError({
28+
configKey: 'logging.transports',
29+
configValue: instanceConfig.get('logging.transports').join(', '),
30+
message: 'You have excluded file logging in your ghost config.' +
31+
'Please add it to your transport config to use this command.',
32+
environment: instance.mode
33+
});
34+
return;
35+
}
36+
37+
let logFileName = path.join(process.cwd(), 'content/logs', `${instance.url.replace(/[^\w]/gi, '_')}_${instance.mode}.log`);
38+
let slice = sliceFile(logFileName);
39+
let prettyStream = new PrettyStream();
40+
41+
prettyStream.pipe(this.ui.stdout);
42+
43+
if (options.follow) {
44+
slice.follow(-options.number).pipe(prettyStream);
45+
return;
46+
} else {
47+
slice.slice(-options.number).pipe(prettyStream);
48+
}
49+
};

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"execa": "0.6.1",
4848
"fkill": "4.1.0",
4949
"fs-extra": "2.1.1",
50+
"ghost-ignition": "^2.8.10",
5051
"greenlock-cli": "2.2.6",
5152
"inquirer": "3.0.6",
5253
"knex-migrator": "2.0.16",
@@ -58,6 +59,7 @@
5859
"rxjs": "5.2.0",
5960
"semver": "5.3.0",
6061
"shasum": "1.0.2",
62+
"slice-file": "1.0.0",
6163
"strip-ansi": "3.0.1",
6264
"symlink-or-copy": "1.1.8",
6365
"update-notifier": "2.1.0",

yarn.lock

+40-6
Original file line numberDiff line numberDiff line change
@@ -1244,9 +1244,9 @@ getpass@^0.1.1:
12441244
dependencies:
12451245
assert-plus "^1.0.0"
12461246

1247-
ghost-ignition@^2.8.2:
1248-
version "2.8.9"
1249-
resolved "https://registry.yarnpkg.com/ghost-ignition/-/ghost-ignition-2.8.9.tgz#8cb2ab812ce5f8e803c3cca869176c6dd0e0098b"
1247+
ghost-ignition@^2.8.10, ghost-ignition@^2.8.2:
1248+
version "2.8.10"
1249+
resolved "https://registry.yarnpkg.com/ghost-ignition/-/ghost-ignition-2.8.10.tgz#7bde7d3d7d87790736f9991fb4f497154e56db14"
12501250
dependencies:
12511251
bunyan "1.8.5"
12521252
bunyan-loggly "1.1.0"
@@ -1257,8 +1257,8 @@ ghost-ignition@^2.8.2:
12571257
lodash "^4.16.4"
12581258
moment "^2.15.2"
12591259
nconf "0.8.4"
1260-
node-uuid "1.4.7"
12611260
prettyjson "1.1.3"
1261+
uuid "^3.0.0"
12621262

12631263
glob-base@^0.3.0:
12641264
version "0.3.0"
@@ -1516,6 +1516,10 @@ inherits@2, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.0, inherits@~2.0.1:
15161516
version "2.0.3"
15171517
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
15181518

1519+
inherits@~1.0.0:
1520+
version "1.0.2"
1521+
resolved "https://registry.yarnpkg.com/inherits/-/inherits-1.0.2.tgz#ca4309dadee6b54cc0b8d247e8d7c7a0975bdc9b"
1522+
15191523
ini@^1.3.0, ini@^1.3.4, ini@~1.3.0:
15201524
version "1.3.4"
15211525
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.4.tgz#0537cb79daf59b59a1a517dff706c86ec039162e"
@@ -2346,7 +2350,7 @@ node-pre-gyp@~0.6.31:
23462350
tar "~2.2.1"
23472351
tar-pack "~3.3.0"
23482352

2349-
node-uuid@1.4.7, node-uuid@~1.4.7:
2353+
node-uuid@~1.4.7:
23502354
version "1.4.7"
23512355
resolved "https://registry.yarnpkg.com/node-uuid/-/node-uuid-1.4.7.tgz#6da5a17668c4b3dd59623bda11cf7fa4c1f60a6f"
23522356

@@ -2678,6 +2682,15 @@ [email protected], readable-stream@^1.1.12:
26782682
isarray "0.0.1"
26792683
string_decoder "~0.10.x"
26802684

2685+
"readable-stream@>=1.0.33-1 <1.1.0-0":
2686+
version "1.0.34"
2687+
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c"
2688+
dependencies:
2689+
core-util-is "~1.0.0"
2690+
inherits "~2.0.1"
2691+
isarray "0.0.1"
2692+
string_decoder "~0.10.x"
2693+
26812694
readable-stream@^2.0.0, "readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.0.5, readable-stream@^2.2.2:
26822695
version "2.2.5"
26832696
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.5.tgz#a0b187304e05bab01a4ce2b4cc9c607d5aa1d606"
@@ -3045,6 +3058,14 @@ [email protected]:
30453058
version "0.0.4"
30463059
resolved "https://registry.yarnpkg.com/slice-ansi/-/slice-ansi-0.0.4.tgz#edbf8903f66f7ce2f8eafd6ceed65e264c831b35"
30473060

3061+
3062+
version "1.0.0"
3063+
resolved "https://registry.yarnpkg.com/slice-file/-/slice-file-1.0.0.tgz#8b6c79d0a50c3fc3c1159957478426443a9000b8"
3064+
dependencies:
3065+
inherits "~1.0.0"
3066+
split2 "^0.2.1"
3067+
through2 "^0.6.5"
3068+
30483069
slide@^1.1.5:
30493070
version "1.1.6"
30503071
resolved "https://registry.yarnpkg.com/slide/-/slide-1.1.6.tgz#56eb027d65b4d2dce6cb2e2d32c4d4afc9e1d707"
@@ -3085,6 +3106,12 @@ spdx-license-ids@^1.0.2:
30853106
version "1.2.2"
30863107
resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-1.2.2.tgz#c9df7a3424594ade6bd11900d596696dc06bac57"
30873108

3109+
split2@^0.2.1:
3110+
version "0.2.1"
3111+
resolved "https://registry.yarnpkg.com/split2/-/split2-0.2.1.tgz#02ddac9adc03ec0bb78c1282ec079ca6e85ae900"
3112+
dependencies:
3113+
through2 "~0.6.1"
3114+
30883115
sprintf-js@~1.0.2:
30893116
version "1.0.3"
30903117
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
@@ -3273,6 +3300,13 @@ text-table@~0.2.0:
32733300
version "0.2.0"
32743301
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
32753302

3303+
through2@^0.6.5, through2@~0.6.1:
3304+
version "0.6.5"
3305+
resolved "https://registry.yarnpkg.com/through2/-/through2-0.6.5.tgz#41ab9c67b29d57209071410e1d7a7a968cd3ad48"
3306+
dependencies:
3307+
readable-stream ">=1.0.33-1 <1.1.0-0"
3308+
xtend ">=4.0.0 <4.1.0-0"
3309+
32763310
through@^2.3.6:
32773311
version "2.3.8"
32783312
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"
@@ -3516,7 +3550,7 @@ xdg-basedir@^3.0.0:
35163550
version "3.0.0"
35173551
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-3.0.0.tgz#496b2cc109eca8dbacfe2dc72b603c17c5870ad4"
35183552

3519-
xtend@^4.0.0:
3553+
"xtend@>=4.0.0 <4.1.0-0", xtend@^4.0.0:
35203554
version "4.0.1"
35213555
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"
35223556

0 commit comments

Comments
 (0)