Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: exposed rotating-file log for json logging #948

Merged
merged 2 commits into from
Aug 31, 2018

Conversation

mlucool
Copy link
Contributor

@mlucool mlucool commented Aug 24, 2018

This both allows for logger type 'rotating-file' and
passing of other options from the config "option" example:

{type: rotating-file, format: json, path: /path/to/log.jsonl, level: http, options: {period: 1d}}

Resolves: #388
Refers: #427

*/
class VerdaccioRotatingFileStream extends Logger.RotatingFileStream { // We depend on mv so that this is there
write(obj) {
const msg = fillInMsgTemplate(obj.msg, obj, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the definition of fillInMsgTemplate ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function fillInMsgTemplate(msg, obj, colors) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I forgot to update my master branch.


let dest;
let destIsTTY = false;
const prettyPrint = (obj) => print(obj.level, obj.msg, obj, destIsTTY) + '\n';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be defined at top as utils ... Please use template literals https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Syntax

let destIsTTY = false;
const prettyPrint = (obj) => print(obj.level, obj.msg, obj, destIsTTY) + '\n';
const prettyTimestampedPrint = (obj) => obj.time.toISOString() + print(obj.level, obj.msg, obj, destIsTTY) + '\n';
const jsonPrint = (obj) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

let destIsTTY = false;
const prettyPrint = (obj) => print(obj.level, obj.msg, obj, destIsTTY) + '\n';
const prettyTimestampedPrint = (obj) => obj.time.toISOString() + print(obj.level, obj.msg, obj, destIsTTY) + '\n';
const jsonPrint = (obj) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

package.json Outdated
@@ -44,6 +44,7 @@
"mime": "2.3.1",
"minimatch": "3.0.4",
"mkdirp": "0.5.1",
"mv": "^2.1.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's optional and this forces it to be there. Their code does if(mv): https://github.com/trentm/node-bunyan/blob/master/lib/bunyan.js#L96

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the ^ please

/**
* A RotatingFileStream that modifes the message first
*/
class VerdaccioRotatingFileStream extends Logger.RotatingFileStream { // We depend on mv so that this is there
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juanpicado I am sure about extending the Logger.RotatingFileStream

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you suggest instead? Reimplementing it is a lot of work and this is public interface so a breaking change would be declarative

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense.

});
} else if (target.type === 'stdout' || target.type === 'stderr') {
dest = target.type === 'stdout' ? process.stdout : process.stderr;
destIsTTY = dest.isTTY;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

destIsTTY it is not used further. ??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used above, which is why those functions are in this scope. It's basically binding them all to the variables in this block scope

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can pass destIsTTY in pretty prettyTimestampedPrint, jsonPrint explicitily. Currently it is not readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll refactor this class. Was trying to avoid that, but it is now unreadable, its time to refactor :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mlucool feel free to refactor the whole logger.js if you wish

stream.writable = true;

let dest;
let destIsTTY = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can be a constant.


if (target.type === 'file') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define dest here. I'll suggest using full name instead of short forms. destination

@@ -46,42 +57,79 @@ function setup(logs) {
return JSON.stringify({...obj, msg}, Logger.safeCycles()) + '\n';
};

if (target.type === 'file') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From line 53 to 58, the following code is duplicated below and not being unused.

    const prettyPrint = (obj) => print(obj.level, obj.msg, obj, destIsTTY) + '\n';
    const prettyTimestampedPrint = (obj) => obj.time.toISOString() + print(obj.level, obj.msg, obj, destIsTTY) + '\n';
    const jsonPrint = (obj) => {
      const msg = fillInMsgTemplate(obj.msg, obj, destIsTTY);
      return JSON.stringify({...obj, msg}, Logger.safeCycles()) + '\n';
    };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh, bad rebase on my part. Will fix

@juanpicado
Copy link
Member

juanpicado commented Aug 25, 2018

/Users/user/projects/verdaccio/src/lib/logger.js
  51:9   error  'dest' is defined but never used                             no-unused-vars
  53:11  error  'prettyPrint' is assigned a value but never used             no-unused-vars
  54:11  error  'prettyTimestampedPrint' is assigned a value but never used  no-unused-vars
  55:11  error  'jsonPrint' is assigned a value but never used               no-unused-vars

✖ 7 problems (4 errors, 3 warnings)

@juanpicado
Copy link
Member

⚠️ Cluster mode

WARNING on cluster usage: Using Bunyan's rotating-file stream with node.js's "cluster" module can result in unexpected file rotation. You must not have multiple processes in the cluster logging to the same file path. In other words, you must have a separate log file path for the master and each worker in the cluster. Alternatively, consider using a system file rotation facility such as logrotate on Linux or logadm on SmartOS/Illumos. See this comment on issue #117 for details.

https://github.com/trentm/node-bunyan#stream-type-rotating-file

@mlucool that deserves to be in the docs as well. As a reference at the bottom below the table

@juanpicado
Copy link
Member

juanpicado commented Aug 25, 2018

It seems does not work, it fills files partially and then are emtpy.

# log settings
logs:
#  - {type: stdout, format: pretty, level: http}
#  - {type: file, path: /Users/user/verdaccio.log, level: trace}
  - {type: rotating-file, format: json, path: /Users/user/verdaccio-rotate.json, level: http, options: {period: 1000ms}}
➜ npm install babel-core

ezgif com-video-to-gif 4

Copy link
Member

@juanpicado juanpicado left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧

@juanpicado juanpicado added this to In Progress in Verdaccio 4 Aug 25, 2018
@mlucool
Copy link
Contributor Author

mlucool commented Aug 25, 2018

@juanpicado @ashishsurana Thanks for the comments, I'll look at this next week.

@mlucool that deserves to be in the docs as well. As a reference at the bottom below the table

Good catch. Want me to also throw on this via checking isWorker?

It seems does not work, it fills files partially and then are emtpy.

That is expected. It is a time based rotate only and will rotate even if there is no new data. In your example, you have it rotating every 1s. Try navigating the website or installing something every second and you'll see that logs continue to have data.

This both allows for logger type 'rotating-file' and
passing of other options from the config "option" example:
{type: rotating-file, format: json, path: /path/to/log.jsonl, level: http, options: {period: 1d}}
@juanpicado juanpicado added this to To do in Refactoring Roadmap via automation Aug 30, 2018
@juanpicado juanpicado removed this from In Progress in Verdaccio 4 Aug 30, 2018
@juanpicado juanpicado added this to the 3.x.x milestone Aug 30, 2018
@juanpicado
Copy link
Member

If there are no changes on logger configuration, it will be shipped with next minor v3.8.0release.

@mlucool
Copy link
Contributor Author

mlucool commented Aug 30, 2018

@juanpicado @ayusharma please re-review.

@juanpicado
Copy link
Member

@mlucool I was on it but .... sadly my ISP decided I have to change my router 😢 ... I'll be offline rest of the week until fix that.

@@ -6647,7 +6647,7 @@ mute-stream@0.0.7:
version "0.0.7"
resolved "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"

mv@~2:
mv@^2.1.1, mv@~2:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mv@^2.1.1, mv@~2: <-- the ^ remains in lock file

Copy link
Member

@juanpicado juanpicado left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@juanpicado juanpicado merged commit 5ca0ca5 into verdaccio:master Aug 31, 2018
Refactoring Roadmap automation moved this from To do to Done Aug 31, 2018
@juanpicado
Copy link
Member

Thanks @mlucool ! great job

@juanpicado
Copy link
Member

@mlucool
Copy link
Contributor Author

mlucool commented Sep 5, 2018

Thanks!

@lock
Copy link

lock bot commented Dec 17, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants