In some cases I might want to use process.stdout in place of console.log when working out a nodejs script. The console.log method works just fine for most typical user case examples, however it does append a line feed at the end of the output each time. Often this might be what I want to happen, however when it comes to having better control over the standard output of a script the write method of the strout stream in the process global is how to go about doing so.
For starters just play around with the console.log method and the process.stdout.write method. WHen doing so there is one majot difference and that is that when using the stdout stream directly a line break will not be added to the end of the output automatically. This allows for me to not do so when it comes to writing scripts that will create output that will be piped into another command and i do not want that extra line break in the output. If I do want to append a line break I can do so by making use of something like the End Of Line property of the os module.
The use of console.lof works okay, but often it is a good idea to have a custom log method. Also when it comes to creating a major project it might even be a good idea to have a log module, that way I can have all the calls of a log method point to the use of a method in that log module. WHen it comes to defining what that log function is, I could use console.log, or I could use the write method of the stdout stream in process. I could also making logging point to some whole other kine of stream completely if I wanted to. So then in this section I will be going over some examples of basic log functions.
A starting point might be to have a log method at the top of a single script. Often many of my simple nodejs projects are just one file so something like this might work just fine.
So it is a little neater to call log each time rather than console.log. However there is more to it than just that, for example if something that I am working on starts to become a little more advanced there is taking the log method and placing it in its own module. I can then use the same log method for all of my modules, and having all logging go to just one place.
The next step from a simple log method would be to make something that is a module that I can pull away into its own file, and then link to from all of my other scripts. When it comes to going all out with something like this there are many little details that I might add to such a module.
One feature that I might add is color support, there are npm packages like chalk that can be used to add color, or another way is to just know the ansi codes to turn color on and off. In any case when it comes to adding color to output that is something that I might want to add for output that will be color to a console, but it might not be what I want to end up going to a file, or some other script when it comes to piping standard output to another script. So it makes sense to have more than one log method,or ways to turn that kind of feature off when it comes to use case examples where I would not want these ansi codes added to the output.
I can then use this log method in ascript like this.
This results in cyan color text for the hello world message, and red color text for errors.
So then that is all when it comes to standard output and nodejs scripts, at least as far as this post goes at least. However there is also the topic of working with standard input in a nodejs script also, for that I have a similar post on the stdin stream of the process global. However to really get the hand of standard output, input, and many other little topics it might be best to start working out some simple actually nodejs project examples.
I do have my main post on the nodejs process global in general that might be work checking out also. There is a lot to be aware of when it comes to this global, so it makes sense to work out at least a few basic examples, and simple projects to get the hang of things.