Function Repository Resource:

ToggleLogInputs

Source Notebook

Automatic logging of executed input cells to a file

Contributed by: Michael Sollami

ResourceFunction["ToggleLogInputs"][]

starts or stops input logging to an autocreated log file.

ResourceFunction["ToggleLogInputs"][file]

starts or stops input logging to a given log file.

Details and Options

ResourceFunction["ToggleLogInputs"] works by toggling a logging process on and off, the current status of which is displayed in an Echo.
ResourceFunction["ToggleLogInputs"] always returns the path of the file to which it logs data, and attempts to create file if it does not exist
ResourceFunction["ToggleLogInputs"] only saves executed input cells to the log, along with commented time stamps indicating when logging started or stopped
LogInput accepts the following options:
"AppendLog"Truewhether to append lines or overwrite log file content
FormatTypeInputFormspecify the format type when logging expressions
PageWidth80specify how wide each line of text is allowed to be in the log file
"PrintLog"Truewhether to print a paneled view of the file when logging stops

Examples

Basic Examples (3) 

Start logging your inputs:

In[1]:=
DeleteFile["session.log"]
In[2]:=
ResourceFunction["ToggleLogInputs"]@"session.log";

Perform work in your session:

In[3]:=
1 + 1
Out[3]=
In[4]:=
2^2
Out[4]=

When you are done, stop logging:

In[5]:=
ResourceFunction["ToggleLogInputs"][];

By default, ToggleLogInputs will append to log files:

In[6]:=
DeleteFile["session.log"]
In[7]:=
ResourceFunction["ToggleLogInputs"]@"session.log";
In[8]:=
"hi";
1/3^2^1^0;
In[9]:=
ResourceFunction["ToggleLogInputs"][]
Out[9]=

When logging is stopped, ToggleLogInputs displays its contents in a panel:

In[10]:=
ResourceFunction["ToggleLogInputs"][]
Out[10]=
In[11]:=
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11;
Factor[x^10 - y^10];
\[Integral]x^2 \[DifferentialD]x;
In[12]:=
ResourceFunction["ToggleLogInputs"][];

Note that each input is written as a line of box data:

In[13]:=
FilePrint@%

Options (1) 

To overwrite an existing log rather than append to it, use the option "AppendLog" False:

In[14]:=
ResourceFunction["ToggleLogInputs"]["session.log", "AppendLog" -> True];
"appending";
ResourceFunction["ToggleLogInputs"][];
In[15]:=
ResourceFunction["ToggleLogInputs"]["session.log", "AppendLog" -> False];
"starting anew";
ResourceFunction["ToggleLogInputs"][];

Applications (1) 

Once logging is stopped, process the log file however you like:

In[16]:=
ins = DisplayForm /@ ToExpression /@ Import["session.log", "Lines"];
DocumentNotebook[ins]
Out[16]=
In[17]:=
nb = NotebookPut@Notebook[Cell[#, "Input"] & /@ ins];
CurrentNotebookImage@nb
In[18]:=

Publisher

Michael Sollami

Version History

  • 1.0.0 – 11 February 2021

Related Resources

License Information