Logging in Fortran
Here’s the translation of the Go logging example to Fortran, formatted in Markdown suitable for Hugo:
Our example demonstrates logging in Fortran. While Fortran doesn’t have a standard logging package like Go, we can create a simple logging module to achieve similar functionality.
In this Fortran example, we’ve created a simple logging module that provides basic functionality similar to the Go logging package. Here’s a breakdown of the code:
We define a
logging
module with alog_message
subroutine that writes timestamped messages to a specified unit (default is standard output).The
set_log_unit
subroutine allows changing the output unit, similar to how we can change the output in the Go example.In the main program, we demonstrate logging to standard output and to a file.
While Fortran doesn’t have built-in support for structured logging like Go’s
slog
package, we show how to include additional information in the log message as a simple string.
To run the program, save it as logging.f90
and compile it with a Fortran compiler:
This will produce output in the console and create a logfile.txt
file with additional log entries.
Note that Fortran’s logging capabilities are more basic compared to Go’s. For more advanced logging in Fortran, you might need to use external libraries or create a more sophisticated custom logging module.