Skip to content

Commit

Permalink
Updating the log file documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
larsvilhuber committed Dec 15, 2024
1 parent 56e87b6 commit 86afed5
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 11 deletions.
25 changes: 20 additions & 5 deletions 04-01-creating_log_files_manually.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,33 @@ close(globallog)
:::{tab-item} Python

```python
% The logging module should achieve this.
import logging
logging.warning('Watch out!')
from datetime import datetime
def track_calls(func):
def wrapper(*args, **kwargs):
with open('function_log.txt', 'a') as f:
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
f.write(f"[{timestamp}] Calling {func.__name__} with args: {args}, kwargs: {kwargs}\n")
result = func(*args, **kwargs)
return result
return wrapper

# Usage
@track_calls
def my_function(x, y,default="TRUE"):
return x + y

my_function(1, 2,default="false")
```

will output

```
WARNING:root:Watch out!
[2024-12-15 12:05:37] Calling my_function with args: (1, 2), kwargs: {'default': 'false'}
```

See also the [Python logging documentation](https://docs.python.org/3/library/logging.html) for controlled output.
:::

::::

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.
While some software (Stata, MATLAB) will create log files that contain commands and output, others (R, Python) will (by default) create log files that contain only output.
8 changes: 7 additions & 1 deletion 04-creating_log_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ I do note that we are typically only looking to document what the statistical co
In almost all cased, the generated log files are simple text files, without any formatting, and can be read by any text editor (e.g., Visual Studio Code, Notepad++, etc.).


If not, ensure that they are (avoid *Stata SMCL* files, for example).
If not, ensure that they are (avoid *Stata SMCL* files, for example).

## Some auxiliary info

> It may be useful at this time to understand how to run code non-interactively.
>
> - [Useful resource for R](https://github.com/gastonstat/tutorial-R-noninteractive/)
2 changes: 2 additions & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Content is [![License: CC BY-NC 4.0](https://licensebuttons.net/l/by-nc/4.0/80x1

The table of contents goes initially from easy to more complex. Each section should be seen as one method of running, with varying levels of "trust" in how robust it is to replicators' environments. Some can be combined, others may not work well together.

> A presentation that may be more up-to-date is available at [presentation/](presentation/).
## TL;DR

Techy lingo for "too long, didn't read". A summary of the most important takeaways will be at the top of each section.
Expand Down
116 changes: 111 additions & 5 deletions presentation/04-creating_log_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,109 @@ log using "`globallog'", name(global) replace text
:::
::::

## Python {auto-animate=true}

:::: {.columns}

::: {.column width=30%}

Create a wrapper that will capture the calls for any function

:::

::: {.column width=70%}


```{.python code-line-numbers="2-9"}
from datetime import datetime
def track_calls(func):
def wrapper(*args, **kwargs):
with open('function_log.txt', 'a') as f:
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
f.write(f"[{timestamp}] Calling {func.__name__} with args: {args}, kwargs: {kwargs}\n")
result = func(*args, **kwargs)
return result
return wrapper
# Usage
@track_calls
def my_function(x, y,default="TRUE"):
return x + y
my_function(1, 2,default="false")
```

:::
::::


## Python {auto-animate=true}

:::: {.columns}

::: {.column width=30%}

Activate the wrapper

:::

::: {.column width=70%}

```{.python code-line-numbers="12"}
from datetime import datetime
def track_calls(func):
def wrapper(*args, **kwargs):
with open('function_log.txt', 'a') as f:
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
f.write(f"[{timestamp}] Calling {func.__name__} with args: {args}, kwargs: {kwargs}\n")
result = func(*args, **kwargs)
return result
return wrapper
# Usage
@track_calls
def my_function(x, y,default="TRUE"):
return x + y
my_function(1, 2,default="false")
```

:::
::::

## Python {auto-animate=true}

:::: {.columns}

::: {.column width=30%}

Ideally, capture the output

:::

::: {.column width=70%}


```{.python code-line-numbers="8"}
# Usage
@track_calls
def my_function(x, y,default="TRUE"):
return x + y
my_function(1, 2,default="false")
# Output
# [2024-12-15 12:05:37] Calling my_function with args: (1, 2), kwargs: {'default': 'false'}
```

:::
::::


## Notes

- More examples
- 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**.
- While some software (Stata, MATLAB) will create log files that contain **commands and output**, others (R, Python) by default will create log files that contain **only output**.


# Creating log files automatically
Expand Down Expand Up @@ -166,13 +265,13 @@ which will create a file `main.log` in the same directory as `main.do`.
To automatically create a log file, run R from the command line using the `BATCH` functionality, as follows:

```bash
R CMD BATCH options infile outfile
R CMD BATCH infile [outfile]
```

> On Windows, you may need to include the full path of R:
```
C:\Program Files\R\R-4.1.0\bin\R.exe CMD BATCH main.R
C:\Program Files\R\R-4.1.0\bin\R.exe CMD BATCH infile [outfile]
```


Expand All @@ -181,10 +280,10 @@ C:\Program Files\R\R-4.1.0\bin\R.exe CMD BATCH main.R
To automatically create a log file, run R from the command line using the `BATCH` functionality, as follows:

```bash
R CMD BATCH options infile outfile
R CMD BATCH main.R
```

This will create a file `main.Rout` in the same directory as `main.R`.
`outfile` is omitted. This will create a file `main.Rout` in the same directory as `main.R`.

## R {auto-animate=true transition="fade"}

Expand All @@ -204,6 +303,13 @@ If you want to prevent R from saving or restoring its environment (by default, `
R CMD BATCH --no-save --no-restore main.R main.$(date +%F-%H:%M:%S).Rout
```

## R {auto-animate=true transition="fade"}

The most output, and the least "acquired" information, is obtained by running the following command:

```bash
R CMD BATCH --debugger --verbose --vanilla main.R main.$(date +%F-%H:%M:%S).Rout
```

## R {.smaller}

Expand Down

0 comments on commit 86afed5

Please sign in to comment.