You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Directly writing console.log statements throughout the apps codebase is bad as you don't have any possibility to centrally deactivate it.
Are there any plans on integrating a $log like feature in Angular2, or even something a bit more advanced. I've quickly seen diary.js but it seems abandoned.
Reacted by Christophe HOARAU, Paulo Henrique da Costa Barros, Alexander Trakhimenok, Mariano Pardo, Vasco Correia, Fabian Kleiser, Aldo Román, bertrandg, Benjamin Schmid, Chris Karcher and 214 more
Reacted by Andrew Spoda
Reacted by Sajith Mantharath, Christian Stewart, Victor Berchet, Prasenjit Purohit, LyokoJeremie, lcc, Jaime Luis Lopez Carratala, Andres Rodriguez, Victor Bianchi, Mehdi Saeedifar and 7 more
Reacted by Alastair Todd, Jeroen Akkerman, Mariusz Beltowski and Gabriel Edgardo Bazán
Not bad idea. Diary is a pretty simple lib, and should still work (with a bit of tinkering) - might be worth bringing into the main repo (and adding Dart support?) @IgorMinar ?
Reacted by Anthony Ciccarello, Damir Murat and Victor Souza
+1 I would like to see this as well. Currently I am trying to create my own LogService.. but am running into a problem wherein the file/line referenced in the console.log is that of the log.service.js file, and not the original file. Until I can solve that problem, I'll just be forced to use the global console everywhere. Anyone have any ideas?
consoleLog() and formatError() come from A1 (slightly modified for ES6):
export class LogService {
enabled = true;
constructor() {
this.enabled = '<%= ENV %>' === 'dev';
var methods = ['assert', 'debug', 'error', 'info', 'log', 'trace', 'warn'];
methods.forEach((method) => {
this[method] = this.consoleLog(method);
});
}
assert(message) {
//
}
debug(message, ...args) {
//
}
error(message, ...args) {
//
}
info(message, ...args) {
//
}
log(message, ...args) {
//
}
trace(message, ...args) {
//
}
warn(message, ...args) {
//
}
private formatError(arg) {
if (arg instanceof Error) {
if (arg.stack) {
arg = (arg.message && arg.stack.indexOf(arg.message) === -1)
? 'Error: ' + arg.message + '\n' + arg.stack
: arg.stack;
} else if (arg.sourceURL) {
arg = arg.message + '\n' + arg.sourceURL + ':' + arg.line;
}
}
return arg;
}
private consoleLog(type) {
if (this.enabled) {
let console = window.console || {},
logFn = console[type] || console.log || noop,
hasApply = false;
let formatError = this.formatError;
// Note: reading logFn.apply throws an error in IE11 in IE8 document mode.
// The reason behind this is that console.log has type "object" in IE8...
try {
hasApply = !!logFn.apply;
} catch (e) {
//
}
if (hasApply) {
return function(...args) {
var _args = [];
args.forEach(function(arg) {
_args.push(formatError(arg));
});
return logFn.apply(console, _args);
};
}
// we are IE which either doesn't have window.console => this is noop and we do nothing,
// or we are IE where console.log doesn't have apply so we log at least first 2 args
return function(arg1, arg2) {
logFn(arg1, arg2 === null ? '' : arg2);
};
}
return function() {
//
};
}
}
Reacted by Travis Olbrich, Giuseppe Mandato, Giorgi, Rahul Bhammarker and ebaersoft
Reacted by Alexander Trakhimenok, Christian Stewart, Anthony Ciccarello, depricated-account-KarlGustav-unimicro, Günter Zöchbauer, Green-Cat, Fabrice Daugan, Grant Hutchins, Lukas Heddendorp, Trotyl Yu and 13 more
Reacted by Alexander Trakhimenok, Roman Lyubimov, Christian Stewart, Anthony Ciccarello, depricated-account-KarlGustav-unimicro, Günter Zöchbauer, Green-Cat, Stephen Friedrich, Fabrice Daugan, Grant Hutchins and 16 more
Reacted by Alexander Trakhimenok, Roman Lyubimov, Christian Stewart, Anthony Ciccarello, depricated-account-KarlGustav-unimicro, Green-Cat, Stephen Friedrich, Fabrice Daugan, Grant Hutchins, Lukas Heddendorp and 16 more
Reacted by Günter Zöchbauer, Anthony Ciccarello, depricated-account-KarlGustav-unimicro, Green-Cat, Zarko Hristovski, Velin Kerkov, Grant Hutchins, Josh Gachnang, Nodarii, Alberto Rosado and 11 more
After finding this request, I walked the internets and found this little library: https://github.com/koroandr/log4ts The docs aren't that good, but the source is easy to understand.
npm install log4ts --save
created a small wrapper around it. For some reason ngOnInit didn't fire so the initialization has to be called manually for now. This can surely be improved, I threw it together just to quickly test the library.
this.logService.log("Something that interests you");this.logService.log.info("Something that interests you");this.logService.log.fatal("Something that interests you");this.logService.log.error("Something that interests you");this.logService.log.debug("Something that interests you");this.logService.log.warn("Something that interests you");this.logService.log.trace("Something that interests you");
@simpss too many dots to call, probably would be better if usage was something like:
this.logInfo("Something that interests you");
this.logFatal("Something that interests you");
this.logError("Something that interests you");
this.logDebug("Something that interests you");
this.logWarn("Something that interests you");
this.logTrace("Something that interests you");
It was the only way I found I could get the appropriate line number to show up. I didn't realize what you wanted to do is to return the bound function from the logger service. I was, at first, trying to return an anonymous function like Angular1's logger.
Reacted by Jordan, John Gilliland and Guilherme Prezzi
Can you please stop using +1 and just hit subscribe, and/or add an emote to peoples responses where you agree or disagree. This +1-itis drives everyone crazy with useless notifications while we all patiently wait for features that are marked for implementation at some point based on the issues labels. This is why subscribe and emotes were added. Yes, everyone wants this feature you're not adding any weight to the conversation unless you're providing some sort of insight into implementation. Lets try and make the next notification for this feature be the actual closing of the issue.
Reacted by Eric Martinez, Guillaume Nury, Oleksandr Tytskyi, Benjamin Charity, Diego Bonura, Arpit Kumar, Kirtan Gajjar, Robin, Joel Hernández, Mathieu Bruguier and 8 more
The implementation of this issue is currently blocked on the fact that Services are not easily tree shakable see here: #17399 (review)
Feel free to suggest a designed where these issues have been reduce if you want to seee this in core sooner.
It's somehow relevant but it's an old issue and I think ppl have found other ways (i.e. external libs or custom impl) and moved ahead. I'm closing this as there are way more important features to be implemented 🙂
looks like this conversation halted back in July and i am a late comer in this conversation. But, checkout @sedeh/smart-console on npm and let me know what you think.
Live demo is on https://smart-console.stackblitz.io/
angular-automatic-lock-bot commented on Sep 14, 2019
Activity
robwormald commentedon Nov 24, 2015
Not bad idea. Diary is a pretty simple lib, and should still work (with a bit of tinkering) - might be worth bringing into the main repo (and adding Dart support?) @IgorMinar ?
jamesmorgan commentedon Feb 27, 2016
I am surprised I couldn't find a
$logequalivant in angular? +1 for adding this feature.zoechi commentedon Feb 28, 2016
In Dart it's
print()and it can be redefined in zones.blaster151 commentedon Mar 2, 2016
I concur, this is needed.
cviccaro commentedon Mar 9, 2016
+1 I would like to see this as well. Currently I am trying to create my own LogService.. but am running into a problem wherein the file/line referenced in the console.log is that of the log.service.js file, and not the original file. Until I can solve that problem, I'll just be forced to use the global console everywhere. Anyone have any ideas?
consoleLog() and formatError() come from A1 (slightly modified for ES6):
sebastianovide commentedon Mar 12, 2016
+1 for $log equivalent
jstuparitz commentedon Mar 29, 2016
+1 for $log
seenimurugan commentedon Apr 22, 2016
+1 for $log equivalent
juristr commentedon Apr 26, 2016
I didn't look into what diary.js does, yet, but given the reactions from people we should maybe revive it? @robwormald ?
marcalj commentedon Jun 29, 2016
Use the thumbs up button on first comment please!
simpss commentedon Jul 10, 2016
After finding this request, I walked the internets and found this little library: https://github.com/koroandr/log4ts The docs aren't that good, but the source is easy to understand.
npm install log4ts --savecreated a small wrapper around it. For some reason ngOnInit didn't fire so the initialization has to be called manually for now. This can surely be improved, I threw it together just to quickly test the library.
log level config:
and usage:
trakhimenok commentedon Jul 11, 2016
@simpss too many dots to call, probably would be better if usage was something like:
gorilla77 commentedon Jul 12, 2016
@cviccaro You can use this to get the "correct" line number:
cviccaro commentedon Jul 13, 2016
@gorilla77 Thanks! I used it like this:
It was the only way I found I could get the appropriate line number to show up. I didn't realize what you wanted to do is to return the bound function from the logger service. I was, at first, trying to return an anonymous function like Angular1's logger.
juristr commentedon Aug 10, 2016
Ben Nadel (@bennadel) just published an article about this topic. Might be interesting for some of you: http://www.bennadel.com/blog/3129-implementing-a-log-inspired-logging-service-in-angular-2-rc-4.htm
20 remaining items
harikrishnan-u01 commentedon Jul 27, 2017
Is there any fix planned for implementing $log as part of Angular 5 release?
jesujcastillom commentedon Aug 2, 2017
+1
mtpultz commentedon Aug 2, 2017
Can you please stop using +1 and just hit subscribe, and/or add an emote to peoples responses where you agree or disagree. This +1-itis drives everyone crazy with useless notifications while we all patiently wait for features that are marked for implementation at some point based on the issues labels. This is why subscribe and emotes were added. Yes, everyone wants this feature you're not adding any weight to the conversation unless you're providing some sort of insight into implementation. Lets try and make the next notification for this feature be the actual closing of the issue.
Toxicable commentedon Aug 3, 2017
The implementation of this issue is currently blocked on the fact that Services are not easily tree shakable see here:
#17399 (review)
Feel free to suggest a designed where these issues have been reduce if you want to seee this in core sooner.
Anish895 commentedon Dec 22, 2017
how to generate .log extension in angular 4 .. Is there any possible way to create and use??
splincode commentedon Jul 26, 2018
@juristr Is the issue relevant?
ocombe commentedon Jul 26, 2018
@splincode please stop spamming old issues asking if they are still relevant, I assume that you have good intentions, but it's annoying.
splincode commentedon Jul 26, 2018
@ocombe I just wanted to know whether to close the old tasks, if they are not relevant
mhevery Recently closed several obsolete tasks, thanks to this
juristr commentedon Jul 26, 2018
It's somehow relevant but it's an old issue and I think ppl have found other ways (i.e. external libs or custom impl) and moved ahead. I'm closing this as there are way more important features to be implemented 🙂
For those interested, creating a custom logger could look like this one here:
https://github.com/angular/angular/blob/master/aio/src/app/shared/logger.service.ts
splincode commentedon Jul 26, 2018
@juristr Thank you for having responded, I am grateful to you!!!
Also I develop an improved version of the logging for Angular
https://github.com/splincodewd/client-logger
msalehisedeh commentedon Jan 19, 2019
looks like this conversation halted back in July and i am a late comer in this conversation. But, checkout @sedeh/smart-console on npm and let me know what you think.
Live demo is on https://smart-console.stackblitz.io/
angular-automatic-lock-bot commentedon Sep 14, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.