Learning how to create an event logger is very useful for a backend application running on Node JS. In this tutorial, we'll cover the Event Emitter in Node.js and create an event logger. If you are just starting to learn Node.js, I recommend starting at the beginning of this Node.js for Beginners playlist: ruclips.net/p/PL0Zuz27SZ-6PFkIxaJ6Xx_X46avTM1aYw
I know that you posted this video a while back but I am trying to lean more about node and web servers etc. and I will say this has been a pleasure to watch and try. I am 70 but I just enjoy dabbling in code. Your video is easy to follow and easy to understand. Thank you for taking the time to show us how to do things and make them work. Your folder missing is a perfect way to trap errors. Cheers
One cool feature about events is that you emit an event once but you can have multiple listeners on it, for example we can have two listeners listening on the "log" event, one of which is responsible for writing the data on the disk and the other is responsible for displaying it in the console, maybe later on we need a third one for sending emails (ex: to developer) when a specific data is logged (ex: uncaught errors/exceptions), the good part here is all of these logging implementations are independent of each others, so we can drop or modify any of them as needed without having to touch the code where the event is emitted. Thanks Dave,
Very good video. Also, we can use the eventEmitter without a class, just initialize the ee const {EventEmiter} = require('events') const myEvent = new EventEmitter();
watching you tutorial video pretty cool. On the other hand, wish to learn how to developing a payment gateway on the E-Commerce when I getting an online order to forward UPS and Federal Express
Hi Dave, Whats the purpose of UUID in the error log? Isn't the datetime unique enough to serve as an identifier?. I am almost finishing this series and have learned so much from your tutorials. Thanks you so much!
Possibly. It all just depends on how you want to track the data. I'm giving one example. Overall, I'm showing concepts and you can apply them differently.
Hi Dave, thanks for the video but I have to say that this was not helpful. You did use events but it wasnt clear why. Why using events when you can just call the exported function instead of calling emit? 95% of the video was puting together the log function instead of explaining events at all. I would have liked to get more in depth knownledge how events work and when to use them. Im my opinion you are totally missusing them.
While I've helped many, I realize my teaching style isn't for everyone and I'm far from perfect. Keep going in this series, and you will see how I use an event in a practical way as we build a REST API.
Learning how to create an event logger is very useful for a backend application running on Node JS. In this tutorial, we'll cover the Event Emitter in Node.js and create an event logger. If you are just starting to learn Node.js, I recommend starting at the beginning of this Node.js for Beginners playlist: ruclips.net/p/PL0Zuz27SZ-6PFkIxaJ6Xx_X46avTM1aYw
cool
This Goat will unquestionably make me as a full stack development. Concise video packed with abundance of information. Thanks alot Dave
I know that you posted this video a while back but I am trying to lean more about node and web servers etc. and I will say this has been a pleasure to watch and try. I am 70 but I just enjoy dabbling in code. Your video is easy to follow and easy to understand. Thank you for taking the time to show us how to do things and make them work. Your folder missing is a perfect way to trap errors. Cheers
One cool feature about events is that you emit an event once but you can have multiple listeners on it,
for example we can have two listeners listening on the "log" event, one of which is responsible for writing the data on the disk and the other is responsible for displaying it in the console,
maybe later on we need a third one for sending emails (ex: to developer) when a specific data is logged (ex: uncaught errors/exceptions),
the good part here is all of these logging implementations are independent of each others, so we can drop or modify any of them as needed without having to touch the code where the event is emitted.
Thanks Dave,
Spot on my friend! Great note 💯🙏
@@DaveGrayTeachesCode it's rather a humble note to a great content my friend 👍👌🚀
Very good video. Also, we can use the eventEmitter without a class, just initialize the ee
const {EventEmiter} = require('events')
const myEvent = new EventEmitter();
What an incredible video! Thank you so much for explaining everything so well :)
Thank you so much! Very well explained! 💛
Thank you so much for this series. It really helped me a lot.
You're welcome! 🚀
Thanks a lot for this Dave! Your courses are amazing, you are so underrated. I will share with everyone I know, and congratulations!
I was on vacation and had to remember some stuff on node, thank God I found you
Brilliant and simple as always. For people to take a first impression about subjects to involve. So we should be a good students. 😊
Thanks a lot for this Dave! for sharing such an amazing content!
Awesome series 👌😀
Glad you think so! 💯
Excellent didactic. Clarity and objectivity. Thank you one more time. It has helped me in my work.
You're welcome, and thank you for the kind words! 🙏
Love node, Awesome. also expecting for express tutorials
Thank you! 🙏 Yes, I'll be covering how to create Node.js only web server next and then the series will introduce Express to follow. 🚀
nice refresher for us
watching you tutorial video pretty cool. On the other hand, wish to learn how to developing a payment gateway on the E-Commerce when I getting an online order to forward UPS and Federal Express
Awesome tutorial for an absolute beginner to nodejs like me. Thx a lot!!!!!
Glad it was helpful!
Dave man...you are my hero
Happy to help!
Awesome tutorial thank you
Glad you liked it! 💯
@@DaveGrayTeachesCode Thank you
Thank you, this is really good tutorial.
You're welcome, Saleem! 🙏
Hi Dave, Whats the purpose of UUID in the error log? Isn't the datetime unique enough to serve as an identifier?. I am almost finishing this series and have learned so much from your tutorials. Thanks you so much!
Possibly. It all just depends on how you want to track the data. I'm giving one example. Overall, I'm showing concepts and you can apply them differently.
Its me. Again. and again thank you
Really amazing
hi dave are you mean common core modules is existed or embbeded in node js previousely
Yes, they are built-in to Node.js
🚀
Sir best resource to learn nodejs and mongodb
My free course covers Node.js, Express.js and MongoDB: ruclips.net/video/f2EqECiTBL8/видео.html
Hi Dave,
thanks for the video but I have to say that this was not helpful. You did use events but it wasnt clear why. Why using events when you can just call the exported function instead of calling emit? 95% of the video was puting together the log function instead of explaining events at all. I would have liked to get more in depth knownledge how events work and when to use them. Im my opinion you are totally missusing them.
While I've helped many, I realize my teaching style isn't for everyone and I'm far from perfect. Keep going in this series, and you will see how I use an event in a practical way as we build a REST API.
the ocnstant variable `EventEmitter` why is this not camel cased???
It is a class. Here is the docs link: nodejs.org/dist/latest-v16.x/docs/api/events.html#class-eventemitter
it's actually console.error(), I tried console.err() in the past too lol.
Yes, 😂 I hope you saw my overlayed note in the video that offered the correction.
what is the real use of EventEmitter ?
It emits (aka releases or sends) events. Reference: nodejs.org/dist/latest-v18.x/docs/api/events.html#class-eventemitter
@@DaveGrayTeachesCode thank you for reply
Why is the output like this?
31.07.2023 01:03:56 function v4(options, buf, offset) {
if (_native.default.randomUUID && !buf && !options) {
return _native.default.randomUUID();
}
options = options || {};
const rnds = options.random || (options.rng || _rng.default)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
rnds[6] = rnds[6] & 0x0f | 0x40;
rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
if (buf) {
offset = offset || 0;
for (let i = 0; i < 16; ++i) {
buf[offset + i] = rnds[i];
}
return buf;
}
return (0, _stringify.unsafeStringify)(rnds);
} Log Event Emitted!
index.js =>
const logEvents = require('./logEvents')
const EventEmitter = require('events')
class MyEmitter extends EventEmitter{ };
const myEmitter = new MyEmitter();
myEmitter.on('log',(msg)=>logEvents(msg))
// setTimeout(()=>{
//Emit Event
myEmitter.emit('log','Log Event Emitted!')
// },2000)
logEvents.js =>
const {format} = require('date-fns')
const {v4:uuid} = require('uuid')
const fs = require('fs')
const fsPromises = require('fs').promises
const path = require('path')
const logEvents = async (message)=>{
const dateTime = `${format(new Date(), 'dd.MM.yyyy\tHH:mm:ss')}`
const logItem = `${dateTime}\t${uuid}\t${message}
`
console.log(logItem)
try{
if(!fs.existsSync(path.join(__dirname,'logs'))){
await fsPromises.mkdir(path.join(__dirname,'logs'))
}
//testing
await fsPromises.appendFile(path.join(__dirname,'logs','EventLogs.txt'),logItem)
}
catch(err){
console.log(err)
}
}
module.exports = logEvents