Logging in COBOL
Here’s the translation of the Go logging example to COBOL, with explanations in Markdown format suitable for Hugo:
IDENTIFICATION DIVISION.
PROGRAM-ID. LOGGING-EXAMPLE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STANDARD-OUTPUT ASSIGN TO DISPLAY
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD STANDARD-OUTPUT.
01 OUTPUT-LINE PIC X(80).
WORKING-STORAGE SECTION.
01 WS-CURRENT-DATE.
05 WS-YEAR PIC 9(4).
05 WS-MONTH PIC 9(2).
05 WS-DAY PIC 9(2).
01 WS-CURRENT-TIME.
05 WS-HOUR PIC 9(2).
05 WS-MINUTE PIC 9(2).
05 WS-SECOND PIC 9(2).
01 WS-TIMESTAMP PIC X(23).
PROCEDURE DIVISION.
MAIN-PROCEDURE.
PERFORM STANDARD-LOGGER
PERFORM CUSTOM-LOGGER
PERFORM FILE-LOGGER
STOP RUN.
STANDARD-LOGGER.
PERFORM GET-TIMESTAMP
DISPLAY WS-TIMESTAMP " standard logger".
GET-TIMESTAMP.
MOVE FUNCTION CURRENT-DATE TO WS-CURRENT-DATE
MOVE FUNCTION CURRENT-TIME TO WS-CURRENT-TIME
STRING WS-YEAR "/" WS-MONTH "/" WS-DAY " "
WS-HOUR ":" WS-MINUTE ":" WS-SECOND
DELIMITED BY SIZE
INTO WS-TIMESTAMP.
CUSTOM-LOGGER.
PERFORM GET-TIMESTAMP
DISPLAY WS-TIMESTAMP " my: from mylog".
FILE-LOGGER.
OPEN EXTEND STANDARD-OUTPUT
PERFORM GET-TIMESTAMP
STRING WS-TIMESTAMP " buf: hello"
DELIMITED BY SIZE
INTO OUTPUT-LINE
WRITE OUTPUT-LINE
CLOSE STANDARD-OUTPUT
DISPLAY "from buflog: " OUTPUT-LINE.
The COBOL standard library provides straightforward tools for outputting logs from COBOL programs. While it doesn’t have built-in logging packages like in some modern languages, we can implement similar functionality using COBOL’s file handling and display capabilities.
In this example, we’ve created a simple logging program that demonstrates different ways of outputting log messages in COBOL.
The
STANDARD-LOGGER
paragraph mimics a standard logger. It outputs a timestamp followed by a message to the console.The
GET-TIMESTAMP
paragraph is a utility that generates a timestamp similar to what you’d see in many logging libraries. It usesFUNCTION CURRENT-DATE
andFUNCTION CURRENT-TIME
to get the current date and time.The
CUSTOM-LOGGER
paragraph demonstrates how you might create a custom logger with a prefix, similar to creating a new logger instance in other languages.The
FILE-LOGGER
paragraph shows how to log to a file (in this case, we’re usingSTANDARD-OUTPUT
as a simplification, but in a real scenario, you’d typically use a separate file).
To run the program, you would compile it and then execute the resulting program:
$ cobc -x logging-example.cob
$ ./logging-example
Sample output might look like:
2023/08/22 10:45:16 standard logger
2023/08/22 10:45:16 my: from mylog
from buflog: 2023/08/22 10:45:16 buf: hello
Note that COBOL doesn’t have built-in support for structured logging or JSON output like some modern languages. For such functionality, you would need to implement it manually or use third-party libraries if available for your COBOL environment.
This example demonstrates basic logging concepts in COBOL, adapting the ideas from the original example to fit COBOL’s paradigm and available features.