 Hello, I hope you're enjoying this series on Linux shell commands, and be sure to check out the full playlist. There should be a link in the description of the video. And today we're going to be looking at logging messages to log files on the system. Now, let's say you're writing a script and it's going to try to do some things. And obviously if there is an error fails, you're going to have some output to the screen to the user. But you may want to log it to a log file for the system, and there are different log files for different things. Just have a record of that so that, you know, the sysadmin can go back and look over it and see where these errors are happening. So let's go ahead and just quickly look at the end of one of these log files. So I'm going to use the tail command, which will show us last ten lines by default of a file. And we're going to say var log messages. That's one of the log files. And you do have to have permission to do this, so I'm going to sudo. And you can see the last ten lines of that log file it tells you. You know, it was on the local host, you know, the user that generated this message. This was a network manager. And then like down here was an example I did, which, so my name's MetalX1000. And then the error message. So let's go ahead and see how you log to that file. You shouldn't just write directly to it by, you know, redirecting your output into it. There's actually a command called logger. And you can do something like logger. This is an error. Obviously, you'd want to give a little more information than that. We do that. And now if we run that same tail command, you can see that there was a, on the date and time, the local host, this user, this is an error was dumped into that file. So let's go ahead and work that into a command. So let's say we wanted to copy, and I'll make up a file here that doesn't exist. I'll say myfile.log, and we want to copy that to our temp folder, because we're backing up to our temp folder, whatever. You know, obviously you don't want to back things up to your temp folder, because it's temporary. Anyway, let's call it backup.log, I won't give it a number, blah, blah, blah. Okay. So we do that. Let's go to fail, obviously, because that file doesn't exist. What we can do is, one way of doing this is checking is doing the double pipe, as I've shown in previous tutorials. That means if the last command fails, do this, and we can say echo, fail. And now, not only do we get the copy command error, but we also get our little fail error, which obviously you'd want to give a little more information than that. But we can also use our log command, so I can say logger with two g's, and I'll hit error. And it's been logged to the system file, so now if I was to go up and tail that out again, you can see that that message get there, and obviously, again, you'd want to give more information than just a fail. But if you wanted to do both, and again, there's different ways to do this. You can do a full if then statement if you're writing this in a script. As a one-liner, I can put this in parentheses and add two commands in here, so I can say echo, fail, which you're getting the output from the copy command in this case anyway. But fail, I'll say logger, fail to copy, file, and we'll say this file, and then I'll hit enter. So in a script, it would try copying this file to here, and it's saying, well, if it fails, give our fail output, which shows right here on the screen, but also log it. And so now we can say, tail that, and you can see right down here that it has been logged. It gives it the date and time, what user was running it, obviously on the local host, and the error message. And you want to be as descriptive as possible without getting too long. And that's just one log file. If I was to list out, here I can list out all the far log files, and you can see there's a bunch of them, blah, blah, blah, for different things. And there's different ways to write to them. So go ahead and look through the man file for logger, so your man logger should bring it up, and it will give you different options while running this command. So look through that, learn a bit, but very simple, just do that base command logs to the messages log, and that's pretty much it. It's not a bad idea if your script is doing anything of importance, and you want to be able to troubleshoot it later to write to the system log file so you have an ongoing record of that. And the system takes care of those, depending on your system set up every so often, it's going to tar those files to compress them down, tar and GZIP them, I believe, is the compression used, and it will keep them for so long so you can go back quite a ways on the Linux system, unlike the default on a Windows system. If it logs anything, once you restart lots of times, that stuff is gone. Luckily, the Linux system by default backs up, and it's just little text files are compressed over time, so you can definitely troubleshoot quite a while back. So I do, as always, thank you for watching. Please visit my website, filmsbychrist.com, that's Chris at the K. There's a link in the description of the video. There you can look through all my videos. You can also go to my scripts, my notes, and search through all my scripts and notes. You can also support me in the support section, either through PayPal or patreon.com for it, slash mail x1000. I'm actually looking into some other options I heard about something new the other day, but I don't want to put it up there. I'll start using it until I know more about it, but different ways to support me. And if you can't support me financially, think about liking, sharing, subscribing, and commenting. Those thumbs up help a lot. Shares definitely, the more views I get, the better. I do thank you for watching. And as always, I hope that you have a great day.