Railway oriented programming is best explained with a visual picture of a railway track as below.
You have a chain of function calls. Every function call can either do what it is supposed to do or give an error. Red represents error and green represents success. It is a natural tendency to imagine a train going from left to right as you see this picture. To align the explanation with that tendency, let us picture this as the sequence of execution returns from the deepest level of function invocation to the topmost level of function invocation. And it is the output from these functions that is flowing through the tracks.
Function at level L4 can return a successful response through the green track or an error through the red track. At level L3, if it got an error from L4, let us assume for simplicity that there is nothing to be done to handle the error in which case the error just keeps flowing through the red track from L4 to L3 to L2 to L1.
On the other hand, if L4 gave a good output, now L3 tries to do it’s part and if L3 fails, then again L3 goes to the red track and goes all the way to L1. If L4, L3, L2 and L1 are all good, then the client who invoked the L1 function gets the right green output.
I had this setup in my code base, and had wanted to log as much context information as needed so as to be able to respond quickly to a production issue. You do not need to pass the context info down to L4 level and have the L4 level log the context information.
Instead, you need to leverage the railway system that you have established and see that all the error information that you needed from the deeper layers have taken a train to L1 and is right there with your context. So, you just log the error information that came through the train along with the context information that you already had at the client that called L1.
Instead of logging at each level, pass the error information up the chain and log only at the topmost level to cut down on the log footprint. Please let me know if there is a difference of opinion here. Thank you for reading.