-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Can winston be used on the front-end (i.e. browser) for logging? #287
Comments
I haven't tried it, but this might help https://github.com/farpoint/meteor-winston-client |
This is related to: #180 |
There is 404 on that link @joshacheson !! |
Do you have any progress with this issue ? |
Based on feedback from #582, it seems that any future PR would need to focus on separating core logic and transports, rather than using shims such as The good news is that the code is mostly clean and well structured, but would need some guidance from core devs on style and approach. Could @indexzero / @pose share your thoughts? |
Any progress with this issue? |
Man, got my hopes really high when I saw package and then noticed no browser support. |
+1 Same or me. Having the same logging lib for front and back also helps. |
I was just browsing this and it seems a shame that even though it has a http transport there doesn't appear to be any standard client for the browser/other. |
Sad that I will have to use bunyan |
any news on this? |
If you really want to use it, you could just write a custom transport for it, for example: const Transport = require('winston-transport');
export default class BrowserConsole extends Transport {
constructor(opts) {
super(opts);
this.name = 'BrowserConsole';
this.levels = {
error: 0,
warn: 1,
info: 2,
debug: 4,
};
this.methods = {
error: 'error',
warn: 'warn',
info: 'info',
debug: 'log',
};
this.level = opts.level && this.levels.hasOwnProperty(opts.level)
? opts.level : 'info';
}
log(method, message) {
setImmediate(() => {
this.emit('logged', method);
});
const val = this.levels[method];
const mappedMethod = this.methods[method];
if (val <= this.levels[this.level]) {
// eslint-disable-next-line
console[mappedMethod](message);
}
}
} Then you can use it in this way: import BrowserConsole from './BrowserConsole';
const { createLogger, transports } = require('winston');
const log = createLogger({
level: 'info',
});
if (process.env.NODE_ENV !== 'production') {
log.add(new BrowserConsole({
level: 'info',
}));
} With this transport, you get a useless warning because it's considered as legacy code. It would also be beautiful to add the possibility to use other console methods ( |
Thank you @chrisvoo, I've tried this solution but got error:
|
@dmitry-salnikov I don't know, btw I cannot understand why this code is using streams. Maybe my use case was too simple. Try to share how you're using it. |
@chrisvoo I've copypasted the
Then I got error, and tried:
Both calls return same error. My environment which makes possible using Node in browser is Meteor.js v1.6 (Node 8.8.1). Also I haven't modified any line of your code snippet. BTW: I've started using winston not so long time ago, so may be doing something wrong. |
@dmitry-salnikov what kind of error do you receive? Like "info is not a function"? Maybe for some reason, it's badly imported. |
@chrisvoo please take a look: The contents of And I agree with you, I feel that something is wrong with import, but can't figure out why :( Could you please share your thoughts on this? |
What's your Winston version? Mine is: "winston": "^3.0.0-rc1",
"winston-transport": "^3.0.1" |
Actually the same
|
Trying to solve this issue I've made another test:
and got the next error: Here is the stack trace and related code (browser screenshot): Seems like something's really badly imported. @chrisvoo could you please take a look? |
In Winston 3.0.0, transports are streams. However, in browserify-stream, |
@Jordan-Hall Wonder if we are having a rc any soon (and thanks). |
@MarcoMedrano Its a fundamental change which would in theory be a new library time i finished it. @pose Whats your view on splitting out the transport layers from the core package? I like Winston but the eco system does need changing |
I'm struggling to get a clear picture of the status of this issue. Can I use Winston in my React app ? I'm actually not so much interested in logging to the browser console - and quite honestly, I don't understand the point in overriding a My situation is that my React app runs in a Docker container behind an nginx / Let's Encrypt proxy, so I have no access to any JavaScript console output; I would therefore like to forward any log output to a syslog server. I have successfully set up a Before I go about hacking some dumb home-made solution, does anyone have some better advice for me ? |
@z00m1n It mostly depends on your use case. I use winston in the browser to log all requests and function calls I make. And if I'm in a production environment I limit the output to only print errors. And using my code and exchanging the console.log statement with something else would work. However, before you write a hacky solution to make this work, I'd propose using sentry. |
It also depends on if you have control over webpack. Sadly this amazing package is out of date architecturally which makes it impossible to truly solve |
@Keimeno have you noticed any odd behavior or performance issues? Really want to use but it has to be uber-stable as some logging will happen in production for my use case... |
@gcperrin Not sure if you can call it a performance issue, but I've been running some benchmarks and gotten the following results: console.info (dev environment); 1.863s for 10.000 logs. (0,1893ms each) logger.info (prod environment); 3.731s for 10.000 logs. (0.3731ms each) This means if you use my function to silence the loggers in production, you will still have synchronous code running for 0.3731ms (potentially even higher). It might not be a performance issue, but if you have a few hundred logs that are silent in production, it might cause your webapp to lag. |
Any way to use winston to persist log into file system in the browser side? I have a React application and want to store the client side logs into file system. Please kindly suggest some thoughts. Thanks in advance. |
In my electron-based application, I am using winston on the node.js (backend) side but would love to be able to use it on the chromium (web) side to stream logs to the same backend file. Not sure if there is an existing strategy/transport to achieve this or not... |
In an isomorphic application, it is possible to use the basic functions of winston if you import it in chunks. And you also need a polyfill for setImmediate if you use basic transports: import createLogger from 'winston/lib/winston/create-logger';
import ConsoleTransport from 'winston/lib/winston/transports/console';
import logform from 'logform';
let logger = createLogger({
transports: [
new ConsoleTransport({
format: logform.format.simple(),
}),
],
});
if (typeof window !== 'undefined') {
if (!window.setImmediate) {
let uniqImmediateId = 0;
const immediateMap = new Map();
const runImmediate = (id) => {
if (immediateMap.has(id)) {
const [callback, args] = immediateMap.get(id);
Reflect.apply(callback, undefined, args);
clearImmediate(id);
}
};
const channel = new MessageChannel();
const registerImmediate = (id) => channel.port2.postMessage(id);
channel.port1.onmessage = (event) => runImmediate(event.data);
window.setImmediate = (callback, ...args) => {
uniqImmediateId++;
immediateMap.set(uniqImmediateId, [callback, args]);
registerImmediate(uniqImmediateId);
return uniqImmediateId;
};
window.clearImmediate = (id) => immediateMap.delete(id);
}
}
logger.info('message'); |
* fix(webapp): Make browsers use browser bundle * Otherwise, they try to import fs, os, etc. only available in node. * fix(bundling): Remove direct and indirect node exports * Otherwise, browsers will fail to import node dependencies. * fix(bundling): Add shims for select functionality for browsers * fs cannot be imported in browser, readJsonWallet is not relevant in browser, so we add a shim for it. * added shim for logging since our logging framework depends on node (winstonjs/winston#287) * fix(bundling): Bundle js output instead of ts * Also switch from iife to cjs format. Otherwise, vite cannot find exports - this may be due to esbuild not understanding all of tsconfig. * fix(bundling): Don't bundle js in parallel with building it
Exact same situation here, I want to use a common logger library file for both the frontend and "backend" Electron processes. The only option that I can think of to use it browser-side in Electron is to make a wrapper library for the I think another option in Electron browser-side is to enable |
We are wrapping the logger on the browser side so that it looks like winston and uses invoke()/handle() to make IPC calls that call the real winston logger on the Electron side. |
Yep, exactly what I've done, too. |
The main obstacle to making this easier is that the File transport is basically baked into Winston core and needs to be separated out, but the Winston project has zero funding to support that work. |
What is the situation on this issue? I would like to work on this problem. By "separating out" do you mean separating into an external package? I researched this a bit and was able to separate the file transport and related tests to a separate package without affecting other unit or integration tests. Also found a minor bug (related to the soon to be legacy file streams). I could finish those and post PRs. |
Yes, splitting packages between winston (core) and winston-file similar to how we have winston-syslog, winston-mongo, winston-logzio, etc. |
Can winston be used on the front-end for logging? I'd like to use Winston for greater front-end logging ability. Can this be done?
The text was updated successfully, but these errors were encountered: