【转载】GNU Screen logging配置

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【转载】GNU Screen logging配置

转载于:GNU Screen logging

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.

  1. 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 HIf 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.
  2. You can use command deflog on to enable logging for all screens.
  3. 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)
  4. How quickly to flash log to the file is determined by command logfile flush secs Default is 10 sec.
  5. 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 stringDefault 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  (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 on

That 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.

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【转载】GNU Screen logging配置

文章的脚注信息由WordPress的wp-posturl插件自动生成



|2|left
打赏

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: