-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a2485d
commit c89e7c4
Showing
6 changed files
with
192 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
(creating-log-files-explicitly)= | ||
# Creating log files explicitly | ||
|
||
We start by describing how to explicitly generate log files as part of the statistical processing code. | ||
|
||
::::{tab-set} | ||
|
||
|
||
:::{tab-item} Stata | ||
|
||
```stata | ||
global logdir "${rootdir}/logs" | ||
cap mkdir "$logdir" | ||
local c_date = c(current_date) | ||
local cdate = subinstr("`c_date'", " ", "_", .) | ||
local c_time = c(current_time) | ||
local ctime = subinstr("`c_time'", ":", "_", .) | ||
local globallog = "$logdir/logfile_`cdate'-`ctime'-`c(username)'.log" | ||
log using "`globallog'", name(global) replace text | ||
``` | ||
|
||
How to potentially do this automatically at each start, see [Stata manual](https://www.stata.com/manuals/gswb.pdf#gswB.3). | ||
|
||
::: | ||
|
||
:::{tab-item} R | ||
|
||
```R | ||
# This will only log output ("stdout") and warnings/messages ("stderr"), but not the commands themselves! | ||
|
||
logfile.name <- paste0("logfile_", Sys.Date(),"-",format(as.POSIXct(Sys.time()), format = "%H_%M"),"-",Sys.info()["user"], ".log") | ||
globallog <- file(file.path(rootdir,logfile.name), open = "wt") | ||
# Send output to logfile | ||
sink(globallog, split=TRUE) | ||
sink(globallog, type = "message") | ||
|
||
## revert output back to the console | ||
sink(type = "message") | ||
sink() | ||
close(globallog) | ||
``` | ||
|
||
::: | ||
|
||
:::{tab-item} MATLAB | ||
|
||
```matlab | ||
% The "diary" function should achieve this. Not a MATLAB expert! | ||
``` | ||
::: | ||
|
||
:::{tab-item} Python | ||
|
||
```python | ||
% The logging module should achieve this. | ||
import logging | ||
logging.warning('Watch out!') | ||
``` | ||
will output | ||
|
||
``` | ||
WARNING:root:Watch out! | ||
``` | ||
|
||
::: | ||
|
||
:::: | ||
|
||
While some software (Stata, MATLAB) will create log files that contain commands and output, others (R, Python) will create log files that contain only output. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
(creating-log-files-automatically)= | ||
# Creating log files automatically | ||
|
||
An alternative (or complement) to creating log files explicitly is to use native functionality of the software to create them. This usually is triggered when using the **command line** to run the software, and thus may be considered an **advanced topic.** The examples below are for Linux/macOS, but similar functionality exists for Windows. | ||
|
||
|
||
:::::{tab-set} | ||
|
||
|
||
::::{tab-item} Stata | ||
|
||
To automatically create a log file, run Stata from the command line with the `-b` option: | ||
|
||
```bash | ||
stata -b do main.do | ||
``` | ||
|
||
which will create a file `main.log` in the same directory as `main.do`. | ||
|
||
:::{warning} | ||
For this to work, the filename cannot include spaces. | ||
::: | ||
|
||
On Windows, follow instructions [here](https://www.stata.com/manuals/gswb.pdf#gswB.5). | ||
|
||
:::: | ||
|
||
::::{tab-item} R | ||
|
||
To automatically create a log file, run R from the command line as follows: | ||
|
||
```bash | ||
R CMD BATCH main.R | ||
``` | ||
|
||
will create a file `main.Rout` in the same directory as `main.R`. | ||
|
||
:::{warning} | ||
If there are other commands, such as `sink()`, active in the R code, the `main.Rout` file will not contain some output. | ||
::: | ||
|
||
:::: | ||
|
||
::::{tab-item} MATLAB | ||
|
||
To automatically create a log file, run MATLAB from the command line as follows: | ||
|
||
```bash | ||
matlab -nodisplay -r "addpath(genpath('.')); main" -logfile matlab.log | ||
``` | ||
|
||
A similar command on Windows would be: | ||
|
||
```bash | ||
start matlab -nosplash -minimize -r "addpath(genpath('.'));main" -logfile matlab.log | ||
``` | ||
|
||
:::: | ||
|
||
::::{tab-item} Julia, Python | ||
|
||
In order to capture screen output in Julia and Python, on Unix-like system (Linux, macOS), the following can be run: | ||
|
||
```bash | ||
julia main.jl | tee main.log | ||
``` | ||
|
||
or | ||
|
||
```bash | ||
python main.py | tee main.log | ||
``` | ||
|
||
which will create a log file with everything that would normally appear on the console using the `tee` command. | ||
|
||
:::: | ||
|
||
::::: | ||
|
||
## Takeaways | ||
|
||
### What this does | ||
|
||
This ensures | ||
|
||
- that your code runs without problem, after all the debugging. | ||
- that your code runs without manual intervention. | ||
- that your code generates a log file that you can inspect, and that you could share with others. | ||
|
||
### What this does not do | ||
|
||
This does not ensure | ||
|
||
- that it will run on somebody else's computer | ||
- because it does not guarantee that all the software is there | ||
- because it does not guarantee that all the directories for input or output are there | ||
- because many intermediate files might be present that are not in the replication package | ||
- because it does not guarantee that all the directory names are correctly adjusted everywhere in your code | ||
- that it actually produces all the outputs | ||
- because some outputs might be present from test runs | ||
|
||
### What to do next | ||
|
||
To solve some of these problems, let's go to the next step. |
Oops, something went wrong.