17db96d56Sopenharmony_ci:mod:`syslog` --- Unix syslog library routines 27db96d56Sopenharmony_ci============================================== 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: syslog 57db96d56Sopenharmony_ci :platform: Unix 67db96d56Sopenharmony_ci :synopsis: An interface to the Unix syslog library routines. 77db96d56Sopenharmony_ci 87db96d56Sopenharmony_ci-------------- 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ciThis module provides an interface to the Unix ``syslog`` library routines. 117db96d56Sopenharmony_ciRefer to the Unix manual pages for a detailed description of the ``syslog`` 127db96d56Sopenharmony_cifacility. 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ciThis module wraps the system ``syslog`` family of routines. A pure Python 157db96d56Sopenharmony_cilibrary that can speak to a syslog server is available in the 167db96d56Sopenharmony_ci:mod:`logging.handlers` module as :class:`SysLogHandler`. 177db96d56Sopenharmony_ci 187db96d56Sopenharmony_ci.. include:: ../includes/wasm-notavail.rst 197db96d56Sopenharmony_ci 207db96d56Sopenharmony_ciThe module defines the following functions: 217db96d56Sopenharmony_ci 227db96d56Sopenharmony_ci 237db96d56Sopenharmony_ci.. function:: syslog(message) 247db96d56Sopenharmony_ci syslog(priority, message) 257db96d56Sopenharmony_ci 267db96d56Sopenharmony_ci Send the string *message* to the system logger. A trailing newline is added 277db96d56Sopenharmony_ci if necessary. Each message is tagged with a priority composed of a 287db96d56Sopenharmony_ci *facility* and a *level*. The optional *priority* argument, which defaults 297db96d56Sopenharmony_ci to :const:`LOG_INFO`, determines the message priority. If the facility is 307db96d56Sopenharmony_ci not encoded in *priority* using logical-or (``LOG_INFO | LOG_USER``), the 317db96d56Sopenharmony_ci value given in the :func:`openlog` call is used. 327db96d56Sopenharmony_ci 337db96d56Sopenharmony_ci If :func:`openlog` has not been called prior to the call to :func:`syslog`, 347db96d56Sopenharmony_ci :func:`openlog` will be called with no arguments. 357db96d56Sopenharmony_ci 367db96d56Sopenharmony_ci .. audit-event:: syslog.syslog priority,message syslog.syslog 377db96d56Sopenharmony_ci 387db96d56Sopenharmony_ci .. versionchanged:: 3.2 397db96d56Sopenharmony_ci In previous versions, :func:`openlog` would not be called automatically if 407db96d56Sopenharmony_ci it wasn't called prior to the call to :func:`syslog`, deferring to the syslog 417db96d56Sopenharmony_ci implementation to call ``openlog()``. 427db96d56Sopenharmony_ci 437db96d56Sopenharmony_ci 447db96d56Sopenharmony_ci.. function:: openlog([ident[, logoption[, facility]]]) 457db96d56Sopenharmony_ci 467db96d56Sopenharmony_ci Logging options of subsequent :func:`syslog` calls can be set by calling 477db96d56Sopenharmony_ci :func:`openlog`. :func:`syslog` will call :func:`openlog` with no arguments 487db96d56Sopenharmony_ci if the log is not currently open. 497db96d56Sopenharmony_ci 507db96d56Sopenharmony_ci The optional *ident* keyword argument is a string which is prepended to every 517db96d56Sopenharmony_ci message, and defaults to ``sys.argv[0]`` with leading path components 527db96d56Sopenharmony_ci stripped. The optional *logoption* keyword argument (default is 0) is a bit 537db96d56Sopenharmony_ci field -- see below for possible values to combine. The optional *facility* 547db96d56Sopenharmony_ci keyword argument (default is :const:`LOG_USER`) sets the default facility for 557db96d56Sopenharmony_ci messages which do not have a facility explicitly encoded. 567db96d56Sopenharmony_ci 577db96d56Sopenharmony_ci .. audit-event:: syslog.openlog ident,logoption,facility syslog.openlog 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ci .. versionchanged:: 3.2 607db96d56Sopenharmony_ci In previous versions, keyword arguments were not allowed, and *ident* was 617db96d56Sopenharmony_ci required. 627db96d56Sopenharmony_ci 637db96d56Sopenharmony_ci 647db96d56Sopenharmony_ci.. function:: closelog() 657db96d56Sopenharmony_ci 667db96d56Sopenharmony_ci Reset the syslog module values and call the system library ``closelog()``. 677db96d56Sopenharmony_ci 687db96d56Sopenharmony_ci This causes the module to behave as it does when initially imported. For 697db96d56Sopenharmony_ci example, :func:`openlog` will be called on the first :func:`syslog` call (if 707db96d56Sopenharmony_ci :func:`openlog` hasn't already been called), and *ident* and other 717db96d56Sopenharmony_ci :func:`openlog` parameters are reset to defaults. 727db96d56Sopenharmony_ci 737db96d56Sopenharmony_ci .. audit-event:: syslog.closelog "" syslog.closelog 747db96d56Sopenharmony_ci 757db96d56Sopenharmony_ci 767db96d56Sopenharmony_ci.. function:: setlogmask(maskpri) 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ci Set the priority mask to *maskpri* and return the previous mask value. Calls 797db96d56Sopenharmony_ci to :func:`syslog` with a priority level not set in *maskpri* are ignored. 807db96d56Sopenharmony_ci The default is to log all priorities. The function ``LOG_MASK(pri)`` 817db96d56Sopenharmony_ci calculates the mask for the individual priority *pri*. The function 827db96d56Sopenharmony_ci ``LOG_UPTO(pri)`` calculates the mask for all priorities up to and including 837db96d56Sopenharmony_ci *pri*. 847db96d56Sopenharmony_ci 857db96d56Sopenharmony_ci .. audit-event:: syslog.setlogmask maskpri syslog.setlogmask 867db96d56Sopenharmony_ci 877db96d56Sopenharmony_ciThe module defines the following constants: 887db96d56Sopenharmony_ci 897db96d56Sopenharmony_ciPriority levels (high to low): 907db96d56Sopenharmony_ci :const:`LOG_EMERG`, :const:`LOG_ALERT`, :const:`LOG_CRIT`, :const:`LOG_ERR`, 917db96d56Sopenharmony_ci :const:`LOG_WARNING`, :const:`LOG_NOTICE`, :const:`LOG_INFO`, 927db96d56Sopenharmony_ci :const:`LOG_DEBUG`. 937db96d56Sopenharmony_ci 947db96d56Sopenharmony_ciFacilities: 957db96d56Sopenharmony_ci :const:`LOG_KERN`, :const:`LOG_USER`, :const:`LOG_MAIL`, :const:`LOG_DAEMON`, 967db96d56Sopenharmony_ci :const:`LOG_AUTH`, :const:`LOG_LPR`, :const:`LOG_NEWS`, :const:`LOG_UUCP`, 977db96d56Sopenharmony_ci :const:`LOG_CRON`, :const:`LOG_SYSLOG`, :const:`LOG_LOCAL0` to 987db96d56Sopenharmony_ci :const:`LOG_LOCAL7`, and, if defined in ``<syslog.h>``, 997db96d56Sopenharmony_ci :const:`LOG_AUTHPRIV`. 1007db96d56Sopenharmony_ci 1017db96d56Sopenharmony_ciLog options: 1027db96d56Sopenharmony_ci :const:`LOG_PID`, :const:`LOG_CONS`, :const:`LOG_NDELAY`, and, if defined 1037db96d56Sopenharmony_ci in ``<syslog.h>``, :const:`LOG_ODELAY`, :const:`LOG_NOWAIT`, and 1047db96d56Sopenharmony_ci :const:`LOG_PERROR`. 1057db96d56Sopenharmony_ci 1067db96d56Sopenharmony_ci 1077db96d56Sopenharmony_ciExamples 1087db96d56Sopenharmony_ci-------- 1097db96d56Sopenharmony_ci 1107db96d56Sopenharmony_ciSimple example 1117db96d56Sopenharmony_ci~~~~~~~~~~~~~~ 1127db96d56Sopenharmony_ci 1137db96d56Sopenharmony_ciA simple set of examples:: 1147db96d56Sopenharmony_ci 1157db96d56Sopenharmony_ci import syslog 1167db96d56Sopenharmony_ci 1177db96d56Sopenharmony_ci syslog.syslog('Processing started') 1187db96d56Sopenharmony_ci if error: 1197db96d56Sopenharmony_ci syslog.syslog(syslog.LOG_ERR, 'Processing started') 1207db96d56Sopenharmony_ci 1217db96d56Sopenharmony_ciAn example of setting some log options, these would include the process ID in 1227db96d56Sopenharmony_cilogged messages, and write the messages to the destination facility used for 1237db96d56Sopenharmony_cimail logging:: 1247db96d56Sopenharmony_ci 1257db96d56Sopenharmony_ci syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_MAIL) 1267db96d56Sopenharmony_ci syslog.syslog('E-mail processing initiated...') 127