OpenLog::usage = "OpenLog[filename] starts logging all input and output to filename."
CloseLog::usage = "CloseLog[] closes the logfile opened by OpenLog[]."

Begin["`Private`"]

logfile=""

OpenLog[filename_String] := (
logfile = OpenWrite[filename];
If[ Head[logfile] =!= String, Return[] ];
AppendTo[$Echo, logfile];
AppendTo[$Output, logfile];
)

CloseLog[] := (
$Echo = Complement[$Echo, {logfile}];
$Output = Complement[$Output, {logfile}];
Close[logfile];
)

End[]
Null