Introduction
When you install complex software you need log of your activities for subsequent analyses and as a record. If you work with screen then one way to get the log is to enable screen logging.
Gnu screen logging is pretty tricky. There are several command with subtle interactions between them. Interactions which are not clear after reading screen manual or manpage.
- To enable logging you can can put the command log on for each window or invoke screen with the option -L (you can do it from .screenrc too). Shortcut for the log on command is Ctrl+a,shift H. If repeated it toggle logging on/off. Message screenlog.1 created appear when log is activated. If file existed message Appennding to file screenlog.1 is displayed for a shot time.
- You can use command deflog on to enable logging for all screens.
- Where to write the file is determined by command logfile. If no command is given the default is used (screelog.%n in the current directory, where %n is screen window number)
- How quickly to flash log to the file is determined by command
logfile flush secs
Default is 10 sec. - Command logtstamp
string
string allow putting timestamps into the log. If time-stamps are turned ‘on’, screen adds a string containing the current time to the logfile after two minutes of inactivity. When output continues and more than another two minutes have passed, a second time-stamp is added to document the restart of the output. You can change this timeout with the second form of the command. The third form is used for customizing the time-stamp string. Default is:
‘-- %n:%t -- time-stamp -- %M/%d/%y %c:%s --\n’
Log on command, shortcut Ctrl+a,H and option -L are by-and-large equivalent
To start logging to default location (or any predefined location if command logfile is present in .screenrc use Ctrl+a,H. or command log on in your .sceenrc script. Just option -L does not start logging.
By default it will create a file with name “screenlog.0” in your home directory, to which log will be written. If you want to stop logging, use the same Ctrl+a,H
Option -L enable logging, but does not start it.
Option -L ( as in screen -L ) enables logging but does not start it |
Logfile statement
logfile statement in your .sceenrc file defines location of log file for all screen windows. The syntax is:
logfile <i>filename</i>
Defines the name the log files will get. The default is screenlog.%n”.
For example
deflog on logfile /tmp/screen-%S-%n.log
you can customize your .screenrc file, adding for instance
logfile /tmp/screenlog-%Y%m%d-%c:%s
The second form changes the number of seconds screen will wait before flushing the logfile buffer to the filesystem. The default value is 10 seconds. For example, of connections that break often, or if you wnat to monitor logfile, you can use:
logfile flush <b>1</b>
With logfile flush 1 we request that every 1 second the output be flushed to the log, making it easier to follow with tail -f.
Logtstamp string
Command logtstamp string string allow putting timestamps into the log. If time-stamps are turned ‘on’, screen adds a string containing the current time to the logfile after two minutes of inactivity. When output continues and more than another two minutes have passed, a second time-stamp is added to document the restart of the output. You can change this timeout with the second form of the command. The third form is used for customizing the time-stamp string. Default is:
‘-- %n:%t -- time-stamp -- %M/%d/%y %c:%s --\n’
Some useful details can be found at the post GNU Screen logtstamp string of End Point Blog by Jon Jensen (July 24, 2013 )
A short note on GNU Screen configuration:
You can add configuration to ~/.screenrc or another configuration file named by -c filename upon invocation, and among the many options are some to enable logging what happens in the screen windows. This is useful when using screen as a reattachable daemonizer.
Consider this configuration:
logfile path/to/screen-output.%Y%m%d.log logfile flush 1 logtstamp on logtstamp after 5 log onThat works nicely. With logfile we specify the name of the logfile, using some % escapes as per "STRING ESCAPES" in the manpage to put the date in the logfile name.With logfile flush 1 we request that every 1 second the output be flushed to the log, making it easier to follow with tail -f.
logtstamp on turns on timestamping which writes a timestamp to the log after a default 2 minutes of inactivity. We shorten that to 5 seconds with logtstamp after 5.
Finally, log on turns on the logging.
Now, what if we want to customize the timestamp? The default looks like this:
-- 0:process-name -- time-stamp -- Jul/24/13 9:09:56 --Which the manpage says can be customized with logtstampt string ..., where the default is
‘-- %n:%t -- time-stamp -- %M/%d/%y %c:%s --\n’.
The manpage earlier says that arguments may be separated by single or double quotes. Doing so with the default shown doesn't work, because a literal \n shows up in the logfile.
The solution I worked out by trial and error is that you must double-quote the string and use an octal escape value \012. Single-quoting that will output a literal backslash 0 1 2, and \n simply is not a recognized escape. Thus our final configuration directive is:
logtstamp string "-- time-stamp -- %Y-%m-%d %0c:%s --\012"which results in output like:
-- time-stamp -- 2013-07-24 09:59:35 --If you use more than one screen window with this configuration, all windows' output will go into the same logfile. Use the %n escape string to include a window number in the logfile name if you'd like them kept separate.
文章的脚注信息由WordPress的wp-posturl插件自动生成