TRANSCRIPCIÓN 🔗
WEBVTT
00:00:05.919 --> 00:00:08.960
Logging lets us know where the problems are happening.
00:00:09.199 --> 00:00:12.160
It also lets us see how our app is being used.
00:00:12.480 --> 00:00:19.440
But if you aren't careful, logging can actually make it harder to understand where the problems are occurring and how your application is being used.
00:00:19.679 --> 00:00:28.399
So let's talk about how to properly implement logging in a way that will actually benefit our application in today's episode of Dev Questions.
00:00:35.600 --> 00:00:38.240
Software development is more than just writing code.
00:00:38.399 --> 00:00:39.759
So let's talk about the rest of it.
00:00:40.000 --> 00:00:43.759
Specifically, let's talk about how to properly implement logging.
00:00:44.000 --> 00:00:45.679
And let's start with an illustration.
00:00:45.920 --> 00:00:54.240
Back when I was a very new IT director at a small college, I was working with my network administrator to implement server logging.
00:00:54.479 --> 00:00:57.280
Up until that point, we didn't have any central logging.
00:00:57.520 --> 00:01:05.840
Errors were discovered by the users or, you know, but we stumbled across them, or maybe if we looked through the event viewer.
00:01:06.159 --> 00:01:13.599
And we had a full network with Active Directory and Exchange Server and SQL Server and File Servers and all the rest.
00:01:14.079 --> 00:01:20.079
So we we found this software that would centrally manage all these logs.
00:01:20.239 --> 00:01:23.120
It would track the different things going on in our servers.
00:01:23.200 --> 00:01:36.000
It would make sure that it looked out for things like, hey, you got low disk space on this server, or hey, this server is hitting a very high CPU all day long, or we've got a bottleneck in memory here, or whatever the case may be.
00:01:36.079 --> 00:01:37.760
We're throwing exceptions or whatever.
00:01:38.000 --> 00:01:42.799
So this software was great, and it could do so much stuff.
00:01:42.959 --> 00:01:53.120
We went to training, we we figured out how to make the whole thing work correctly, how to get the most out of that software, and then we implemented it in I think it was like 20 servers.
00:01:53.280 --> 00:02:11.840
Um and so we we implemented all these different servers, we had it, we set it up and configured it, so we made sure we looked at all the different errors, all different issues, and made sure that we were tracking all the things we needed to in order to have a really good bird's eye view of what's going on with the entire network.
00:02:13.039 --> 00:02:14.639
And we barely ever used it.
00:02:14.800 --> 00:02:15.759
And here's why.
00:02:16.080 --> 00:02:26.319
Because the first day, my network administrator was getting the emails for all of the errors, all the big problems he'd get the emails for.
00:02:26.560 --> 00:02:37.520
And his inbox was flooded to the point that he created a rule that said, hey, if it's coming from the logging set server, put it in a different folder because I can't even see my normal emails.
00:02:38.319 --> 00:02:39.680
Then what do you think happened?
00:02:39.840 --> 00:02:56.159
At first, we started going through those emails, but then some of them were like, hey, that that you know, that server is getting low on disk space, but we knew that, and that's not some address, or hey, that server is kind of you know going at 80% plus of CPU usage, but that's a good thing.
00:02:56.240 --> 00:02:57.280
We want it to be there.
00:02:57.520 --> 00:03:03.120
And so we started seeing all these issues where for some servers that would have been an issue, but for others it's not.
00:03:03.199 --> 00:03:05.520
And so we started ignoring issues.
00:03:05.759 --> 00:03:11.280
But then we had a hard time finding the issues that were really issues.
00:03:12.080 --> 00:03:19.199
So what we found out is just because an issue seems critical in a vacuum doesn't mean it actually is.
00:03:19.520 --> 00:03:23.680
And if you're not gonna do something about it, then it's not actually an issue.
00:03:24.000 --> 00:03:28.719
So either critical alerts need to be dealt with or they need to be downgraded.
00:03:28.879 --> 00:03:32.000
Otherwise, you drown out the real critical alerts.
00:03:32.159 --> 00:03:38.240
And that's really hard to do because when you're looking at things, you're saying that is an issue.
00:03:38.479 --> 00:03:41.280
But if you're not gonna deal with it, is it?
00:03:41.680 --> 00:03:44.479
I mean, it may be something that you want to address.
00:03:44.560 --> 00:03:56.080
It may be on your, I would love to take care of that list, but unless it's an issue you're gonna say, yes, I'm going to address that now, it's not really something you should have in your critical alert list.
00:03:56.560 --> 00:04:01.039
And so what happened was his inbox was constantly full of alerts.
00:04:01.360 --> 00:04:07.840
And we just never seemed to do a good job of figuring out which ones are critical, which ones aren't.
00:04:08.000 --> 00:04:15.360
And so, yes, we found a problem when a user reported something, we could go to the log server and go, oh, yep, that issue was there.
00:04:15.919 --> 00:04:23.120
We got the alert for it 18 times, but it got lost in the middle of the thousands of other alerts that were going off.
00:04:23.360 --> 00:04:31.360
And so just because those things seemed like they should be logged doesn't mean they should have been.
00:04:31.519 --> 00:04:34.800
We should have been logging just what we're going to fix.
00:04:35.199 --> 00:04:38.160
So let's talk about how to set up logging that works.
00:04:38.319 --> 00:04:42.000
Now, we're talking more about logging inside of your application.
00:04:42.160 --> 00:04:47.120
So this is not exactly about server logs, but they the things still apply.
00:04:47.600 --> 00:04:51.279
So let's talk about how to set up the logging that actually works for you.
00:04:51.519 --> 00:04:59.199
Number one, only mark
Load full transcript
issues as critical if you absolutely need to do something about them.
00:05:00.319 --> 00:05:03.120
Too many people say, well, that's a big deal.
00:05:03.439 --> 00:05:04.319
Is it though?
00:05:04.560 --> 00:05:10.720
Again, if it's a big deal but you don't do anything about it, it's not a big deal.
00:05:11.199 --> 00:05:13.920
It that's just information at that point.
00:05:14.160 --> 00:05:21.199
If you're not going to act on it, there's no reason to make it a critical issue, make it an informational issue.
00:05:21.360 --> 00:05:25.519
And yes, I know sometimes you may go, but that's that's that's important.
00:05:26.480 --> 00:05:29.600
It's only important if you do something about it.
00:05:29.839 --> 00:05:31.600
That's really what it comes down to.
00:05:31.759 --> 00:05:35.519
It doesn't matter what you say, it's what you do.
00:05:35.759 --> 00:05:38.000
This is a fact for all of life.
00:05:38.240 --> 00:05:41.439
If it doesn't matter what you say, it matters what you do.
00:05:41.600 --> 00:05:46.160
So if you say this is critical, but then you do nothing, it's not critical.
00:05:46.480 --> 00:05:55.920
So be very careful to only bubble up things that are critical, things that you're actually going to do something about.
00:05:56.399 --> 00:06:09.920
Number two, only log errors as errors if they're unexpected or need to be addressed, such as, you know, like, hey, this service has crashed.
00:06:10.079 --> 00:06:11.279
Okay, that needs to be addressed.
00:06:11.360 --> 00:06:13.279
And that's that's an error that needs to be addressed.
00:06:13.439 --> 00:06:21.120
Um, if there's an exception that happens, but it's not something we're gonna address, then don't log it as an error.
00:06:21.199 --> 00:06:24.800
Um, even though the error logs are the ones that take the exceptions.
00:06:24.959 --> 00:06:26.959
And you might go, well, I have to put them there.
00:06:27.120 --> 00:06:28.160
No, you don't.
00:06:28.480 --> 00:06:34.319
So you don't need to log the network connection was went down for the user.
00:06:34.480 --> 00:06:39.360
You don't need to log that unless you need to know, oh, the network is going down in this area.
00:06:39.600 --> 00:06:50.160
For example, if you have microservices and two different microservices, maybe one's an API and the other one's talking to the API, maybe that network connection is all internal.
00:06:50.560 --> 00:06:53.759
And if that goes down, yeah, you should know about that.
00:06:53.920 --> 00:06:55.519
And that would be something critical.
00:06:55.759 --> 00:07:06.319
But if the user is on their laptop and they go through a tunnel and they lose the connection to the cell tower and it comes back up afterwards, that's not an issue that you need to be logging.
00:07:06.639 --> 00:07:16.160
So even though it might throw an exception on the user's machine, even though it might be triggered as an error, you need to go, that's not really an error.
00:07:16.480 --> 00:07:26.000
And so this is where being granular about catching these exceptions and going, this one's important, this one's not, is important.
00:07:26.720 --> 00:07:41.040
If you're doing a uh a try catch and that's catching EX, you know, the raw exception ex where you're not specifying which exceptions and you're just saying, hey, make every one of these an error, that's not very efficient.
00:07:41.120 --> 00:07:43.600
It's gonna clog up your errors and your logs.
00:07:43.920 --> 00:07:49.199
So maybe at first you would do that, but then figure out, oh, we're getting these network drop-offs.
00:07:49.279 --> 00:07:51.199
And those network drop-offs aren't really a big deal.
00:07:51.360 --> 00:08:08.319
Well, then in your catch, have another catch before your generic exception ex that's like network exception or whatever it is that's the actual network error, catch that one and maybe let the user know, hey, you don't have a network connection right now.
00:08:08.480 --> 00:08:09.839
But don't log that.
00:08:10.000 --> 00:08:14.560
There's no reason to log that if you're not going to do something with it.
00:08:15.680 --> 00:08:22.959
Number three, only log warnings that are precursors to actual errors or critical issues.
00:08:23.279 --> 00:08:34.000
If there's something that is an indicator that something is going to happen in the future and it will be a major issue in the future, that's a great warning.
00:08:34.320 --> 00:08:39.919
So a simple example might be, hey, I noticed we're getting low on disk space.
00:08:40.240 --> 00:08:40.559
Okay?
00:08:41.039 --> 00:08:46.639
That's something that should be a warning because that's leading up to, hey, we're out of disk space.
00:08:46.960 --> 00:08:52.480
So, yes, that could be a good warning because, again, you're going to do something about it.
00:08:52.720 --> 00:09:04.960
Maybe you won't do something about it right away, but you'll do something about it in a week or within the next few days as you get time, because that warning let you know before it became a critical issue.
00:09:05.279 --> 00:09:15.200
So warnings are good to have, but again, don't log things as warnings that aren't precursors to actual errors or critical issues.
00:09:15.440 --> 00:09:22.480
If it's just a, hey, the user doesn't like us, or, you know, hey, the user put bad data in, these are not warnings.
00:09:22.639 --> 00:09:28.080
This is just information that you've elevated wrongly to a warning level.
00:09:28.399 --> 00:09:33.759
So again, it has to be based upon something that you're going to do something about.
00:09:34.080 --> 00:09:38.159
If you've noticed a theme so far, this is what logs are for.
00:09:38.480 --> 00:09:44.240
Logs are not for the pack rats, the digital pack rats who want to store all this information.
00:09:44.399 --> 00:09:46.000
That's not what logs are for.
00:09:46.320 --> 00:09:48.960
Logs are for doing something with.
00:09:49.039 --> 00:09:53.600
And if you're not doing something with your logs, you're not
using them correctly.
00:09:53.919 --> 00:10:02.000
So, number four, understand your application's importance and adjust what you capture and where.
00:10:03.440 --> 00:10:04.960
What do I mean by this?
00:10:05.120 --> 00:10:08.159
Well, not all applications are created equal.
00:10:08.399 --> 00:10:11.360
Some applications are mission critical applications.
00:10:11.519 --> 00:10:17.759
I worked on a software as a service for a company that their whole line of business was a software as a service.
00:10:17.919 --> 00:10:21.519
If that software as a service went down, then we didn't get paid.
00:10:21.600 --> 00:10:26.399
All of our customers wouldn't pay us because we're not actually providing them what they need.
00:10:26.559 --> 00:10:28.320
The whole business would shut down.
00:10:28.559 --> 00:10:29.919
That's mission critical.
00:10:30.000 --> 00:10:32.480
And that application was logged differently.
00:10:32.720 --> 00:10:45.440
Now, I've used other or built other applications where it supported the development staff, where it made deployments easier, or it made cleanup easier, or it made simple tasks, documentation easier.
00:10:45.679 --> 00:10:47.919
These are not important applications.
00:10:48.159 --> 00:10:58.480
These are support applications that we could get by without or that are nice to have, but no one is coming in the weekend to fix.
00:10:59.120 --> 00:11:04.240
So you need to adjust your application's logs accordingly.
00:11:04.559 --> 00:11:13.440
So just because this application has a critical failure doesn't mean it's it's crazy important to do.
00:11:13.600 --> 00:11:21.360
Again, you're not coming out on a Sunday to fix this application if it's not an important application.
00:11:21.679 --> 00:11:29.840
So you need to understand your application's importance to the organization and adjust your logging accordingly.
00:11:30.080 --> 00:11:31.759
Also, where you log.
00:11:32.480 --> 00:11:37.440
Because there's going to be the central place that everybody should look at.
00:11:37.600 --> 00:11:39.039
Again, read your logs.
00:11:39.120 --> 00:11:46.000
Um, everyone should look at that tells you what's going on, what's critical, what's an error, what's a warning.
00:11:46.159 --> 00:11:49.679
Those are things you should be looking at probably on a daily basis.
00:11:49.919 --> 00:11:55.360
You should be aware of what's going on and be fixing those things because they're priorities.
00:11:55.679 --> 00:12:01.200
But if you have minor applications, they probably shouldn't go in the same system.
00:12:01.440 --> 00:12:08.240
Maybe you should be logged to a text file, or maybe you should log to a secondary server that you check once in a while when you have time.
00:12:08.399 --> 00:12:18.240
When you're looking to make a change to a secondary system, you also look and see, hey, are there any critical or error warnings in there that I should address as well?
00:12:18.720 --> 00:12:24.159
So where you log is also important because also logging is expensive.
00:12:24.399 --> 00:12:33.679
And you don't need to be logging lots of messages from non-critical systems into your main logging system that's going to cost more money.
00:12:33.840 --> 00:12:46.320
And again, it's going to clog up that main system with a whole bunch of critical error and warnings that aren't actually critical errors or warnings that need to be dealt with right away.
00:12:46.559 --> 00:12:52.639
So make sure not to clog up your systems with too many false positives.
00:12:52.879 --> 00:12:55.919
Try very hard to have zero false positives.
00:12:56.240 --> 00:13:05.759
Number six, do not capture information you're not going to act on every day until unless you're investigating an issue.
00:13:05.840 --> 00:13:06.080
Okay.
00:13:06.320 --> 00:13:11.759
So when you're capturing information, don't gather information you're not going to act on.
00:13:12.080 --> 00:13:18.159
So if you're saying, well, I need to know what the user does when they use the application, do you?
00:13:18.559 --> 00:13:20.159
Or is that more like a testing thing?
00:13:20.240 --> 00:13:22.320
Or is that more like a sometimes thing?
00:13:22.559 --> 00:13:34.879
And that's where you have things like information logs where you can turn those on just for investigating an issue, or just for um, you know, trying to figure out how users are using your application.
00:13:35.039 --> 00:13:36.320
Not all the time.
00:13:36.879 --> 00:13:42.240
So don't log information, don't capture things that you aren't going to act on.
00:13:42.559 --> 00:13:45.759
That also means what types of data you capture.
00:13:46.159 --> 00:13:49.039
So this might be, you know what?
00:13:49.279 --> 00:13:56.480
We need to know that we're out of disk space, but we don't need to know what you were doing when you found out you're out of disk space.
00:13:56.559 --> 00:13:57.679
We don't need to know that information.
00:13:57.840 --> 00:14:03.120
We just need to know that the system went, hey, I try to save a file and we're out of disk space.
00:14:03.279 --> 00:14:04.240
That's all you need to know.
00:14:04.399 --> 00:14:08.000
You don't need to know who was who was trying to do something.
00:14:08.480 --> 00:14:09.759
That's irrelevant information.
00:14:09.919 --> 00:14:12.320
Capture the information you're actually going to act on.
00:14:12.480 --> 00:14:21.360
And again, you can use those lower log levels for when you're investigating issues or investigating how the system is used, et cetera.
00:14:21.519 --> 00:14:22.399
You can turn those on.
00:14:22.559 --> 00:14:31.759
Maybe you log them somewhere else, or maybe just make sure they're filtered out of the main system and only used for those investigations.
00:14:32.639 --> 00:14:39.440
Number seven, do not save data beyond when you will reasonably use it.
00:14:40.080 --> 00:14:44.080
I have found that in most cases, 30 days is about all you need.
00:14:44.399 --> 00:14:48.159
Because if you get
beyond that, you're not actually using the data.
00:14:48.320 --> 00:14:58.320
First of all, if you have a critical issue that stays in the critical issues list for over 30 days, you're not using the critical data system correctly.
00:14:58.559 --> 00:15:00.960
You need to have addressed it before now.
00:15:01.120 --> 00:15:05.279
That needs to be turned into a ticket that needs to be starting to be worked on right away.
00:15:05.600 --> 00:15:09.759
So if you have data for more than 30 days, why?
00:15:10.159 --> 00:15:11.120
Why is it there?
00:15:11.279 --> 00:15:13.519
Why is it not converted into an issue?
00:15:13.759 --> 00:15:16.720
For the most part, delete it after 30 days.
00:15:16.799 --> 00:15:18.639
That's all you need at most.
00:15:19.600 --> 00:15:27.200
So you can have a log system that turns over quite frequently that even handles things like hey, we had Christmas break and we're off for a week.
00:15:27.279 --> 00:15:29.360
No problem, because you have 30 days.
00:15:29.519 --> 00:15:32.799
You still have 21 days of usable data.
00:15:33.120 --> 00:15:41.360
So there's still a lot of data there for you, even if you have breaks, even if you have people go on vacation, even if you have both combined at the same time.
00:15:41.600 --> 00:15:46.960
Like 30 days is a lot of data if you were acting on it.
00:15:47.279 --> 00:15:56.080
The times when I've seen people say, I need more than 30 days, is when they are not acting on it and they are only looking at it every once in a while.
00:15:56.320 --> 00:15:59.279
And they say, Well, I need to know historically how things are working.
00:15:59.519 --> 00:16:05.679
You should have been fixing these problems, so historically shouldn't matter, because you should have fixed these problems by now.
00:16:06.000 --> 00:16:15.440
So, therefore, if you have a critical error or critical issue that's happened over 30 days long, then you're doing something wrong.
00:16:15.759 --> 00:16:18.559
Or it's it's in a ticket and you're trying to fix it.
00:16:18.720 --> 00:16:20.240
But at that point, it's already on tickets.
00:16:20.320 --> 00:16:22.240
So why do you need it in a logging system?
00:16:22.639 --> 00:16:27.759
So try not to save data beyond when it's reasonably useful.
00:16:27.919 --> 00:16:36.159
Otherwise, what happens is you become a digital pack rat and you got so much information there that you just never use it, but it just keeps getting more and more expensive to store.
00:16:36.320 --> 00:16:42.080
And it can actually cause some other problems where you are storing things beyond when you should.
00:16:42.639 --> 00:16:47.120
So don't save data beyond when it's reasonably useful.
00:16:47.440 --> 00:16:54.320
Number eight, do not log sensitive data unless it's absolutely necessary.
00:16:55.200 --> 00:17:06.079
This is one that often trips people up, and this is where a lot of security breaches can come in because you're logging the sensitive data and taking it out of a secure
system and putting it into a less secure system.
00:17:06.319 --> 00:17:09.599
So, first of all, your logging system should be pretty secure.
00:17:09.839 --> 00:17:19.680
And then you should also try not to include anything that's of person identified information or other sensitive data in your logs.
00:17:20.480 --> 00:17:21.759
Yes, there's exceptions.
00:17:21.920 --> 00:17:36.079
Yes, there are times when you might have to, but in that case, you need to make sure that your logging system is locked down secure, and it's also in compliance with all the regulations around forgetting a user if they ask you to.
00:17:36.240 --> 00:17:42.000
That means you need to go through your log system and clean out those logs, referring to that user or change that data.
00:17:42.319 --> 00:17:48.079
So being compliant is harder if you're logging sensitive information.
00:17:48.319 --> 00:17:50.799
So maybe try not to.
00:17:50.960 --> 00:18:01.039
And again, these regulations are there for a reason, because people are putting sensitive data in systems that aren't as secure because they think, well, it's just a logging system.
00:18:01.359 --> 00:18:06.400
Or it's not as protected because, well, everyone has to have access to it, right?
00:18:06.640 --> 00:18:13.279
And then next thing you know, you're leaking sensitive data and endangering your users because you were sloppy.
00:18:13.440 --> 00:18:14.559
Don't do that.
00:18:15.279 --> 00:18:17.359
So number nine.
00:18:18.319 --> 00:18:23.039
Number nine is don't log to the browser's console in production.
00:18:23.839 --> 00:18:33.359
This is one where you know, if you're using something like uh React or Angular or Vue, uh, or even like Blaze WebAssembly, where you are front-end fully.
00:18:33.759 --> 00:18:36.960
Try really hard not to put console log statements there.
00:18:38.079 --> 00:18:40.240
Use an actual logging service.
00:18:40.400 --> 00:18:50.559
Now, sometimes that logging service is not available, where maybe you have a network issue or other thing where you need to log or tell a user in some way, try and tell the user directly.
00:18:50.720 --> 00:18:54.960
Don't try very hard not to put it into the console log.
00:18:55.359 --> 00:19:02.240
We really don't want to have to ask users to open up the developer console, look at the actual console message, and figure out the message says.
00:19:02.400 --> 00:19:03.279
That's not good.
00:19:03.519 --> 00:19:07.839
So if we're not asking them to do that, then why are you putting those messages in there?
00:19:08.079 --> 00:19:12.400
Because the other thing is those messages can leak information about what's going on.
00:19:12.559 --> 00:19:21.279
Now, everything you do in the browser is going to be unsecured, or at least unsecured as for that person.
00:19:21.440 --> 00:19:26.880
So if that person logged in and they have their credentials, well, sure, they have access to all of their things.
00:19:27.039 --> 00:19:33.200
But that also means that any code that runs in the front end is going to be completely exposed to the user.
00:19:33.279 --> 00:19:35.680
They can read all the code, they can read all the data.
00:19:35.839 --> 00:19:43.599
So it's not quite the same as logging like a C sharp applications information and displaying that.
00:19:43.680 --> 00:19:52.559
You know, if you have a Blazor uh web app where you're showing the server side logs, yeah, it's more that's more information leakage than a console log would be.
00:19:52.720 --> 00:19:55.599
But still, don't put it in the console logs.
00:19:55.680 --> 00:19:56.720
Not if you can help it.
00:19:56.880 --> 00:19:58.160
There are exceptions.
00:19:58.319 --> 00:19:59.279
There are exceptions.
00:19:59.599 --> 00:20:01.839
You know, no pun intended.
00:20:02.000 --> 00:20:04.559
But there are exceptions to that rule.
00:20:04.880 --> 00:20:09.519
But for the most part, try not to put um errors in your console.
00:20:09.599 --> 00:20:16.880
Try and expose it to the user or send it to a logging system if you're going to act on it.
00:20:17.200 --> 00:20:20.400
If your logs have too much information, they become useless.
00:20:20.640 --> 00:20:25.279
If your application treats too many things as important, nothing is important.
00:20:25.759 --> 00:20:31.359
And if you aren't addressing the actual issues as they come up, you're going to hide the real issues.
00:20:31.599 --> 00:20:35.680
You need to make sure you're only looking at logs you'll actually act upon.
00:20:35.920 --> 00:20:37.680
That's how you protect your applications.
00:20:37.839 --> 00:20:43.759
That's how you improve your user experience, and it's how you ensure you'll actually get value out of your logs.
00:20:44.000 --> 00:20:44.880
Thanks for listening.
00:20:45.119 --> 00:20:46.960
As always, I am Tim Corey.