Does log4j2.properties file support parameters - apache-spark

Currently we use property.basePath = ${spark.yarn.app.container.log.dir}
We have use case where we are thinking to use common log42.properties file. Wanted to know if there is support to pass app_name as a parameter/argument to be replaced in the log4j2.properties file
Sample lines in log4j2properties file:
appender.console.type = Console
appender.console.name = consoleLogger
logger.app.name = ${appName}
logger.app.appenderRef.console.ref = consoleLogger

Related

How to configure log4j.properties for embedded Jetty server?

Currently working with a web page built to use the embedded Jetty server and unable to modify the code to turn on tracing.
As a result, turning on the tracing is being attempted via log4j.properties but unable to obtain any trace information.
Here is the error attempting to be traced. (Error occurs within : JspServletWrapper)
org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.index_jsp+
at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:197)+
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:413)+
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)+
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)+
at org.eclipse.jetty.jsp.JettyJspServlet.service(JettyJspServlet.java:106)+
JETTY_PATH = /usr/jetty_home
In the JETTY_PATH directory, the log4j.properties was created and since the error occurs within the JsServletWrapper, the file contains the following :
Note : Had to use an example from https://howtodoinjava.com/log4j/how-to-configure-log4j-using-properties-file/ as I haven't previously configured this type of file.
log4j.rootLogger=DEBUG, filer
log4j.appender.filer=org.apache.log4j.FileAppender
log4j.appender.filer.layout=org.apache.log4j.PatternLayout
log4j.appender.filer.layout.ConversionPattern=[%d] %p %c - %m%n
log4j.appender.filer.File=/tmp/tmp/jetty.log
log4j.appender.filer.append=true
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=5
log4j.logger.org.apache.jasper.servlet.JspServletWrapper=DEBUG, filer <----Class attempting to trace
Is the log4j.properties file valid and/or in the correct location?
Is there some other configuration step that required to obtain tracing via the log4j.properties for an embedded Jetty server?
Edited to add the Juli stuff.
The logging.properties is placed in the $JETTY_PATH (not sure if correct location)
cat logging.properties
handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4admin.org.apache.juli.FileHandler, 5host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
.handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = /tmp/tmp/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina.
2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = /tmp/tmp/logs
2localhost.org.apache.juli.FileHandler.prefix = localhost.
3manager.org.apache.juli.FileHandler.level = FINE
3manager.org.apache.juli.FileHandler.directory = /tmp/tmp/logs
3manager.org.apache.juli.FileHandler.prefix = manager.
4admin.org.apache.juli.FileHandler.level = FINE
4admin.org.apache.juli.FileHandler.directory = /tmp/tmp/logs
4admin.org.apache.juli.FileHandler.prefix = admin.
5host-manager.org.apache.juli.FileHandler.level = FINE
5host-manager.org.apache.juli.FileHandler.directory = /tmp/tmp/logs
5host-manager.org.apache.juli.FileHandler.prefix = host-manager.
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.FileHandler
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/admin].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/admin].handlers = 4admin.org.apache.juli.FileHandler
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 5host-manager.org.apache.juli.FileHandler
org.apache.jasper.servlet.JspServletWrapper = FINE <-----Class attempting to trace.

Dynamic filename for file handler

I am trying to set up a logger that will write a new timestamped log file every time the application is run in a specific directory.
for example what I am trying to do is
timestampFilename = time.strftime("runtimelog%b_%d_%Y_%H:%M:%S.txt")
fh = logging.FileHandler(r'C:\my\folder\logs\'+timestampFilename, mode='w')
An example tweeked from the Logging Cookbook:
import logging
import os
from datetime import datetime
# create logger with 'spam_application'
logger = logging.getLogger('MYAPP')
logger.setLevel(logging.DEBUG)
# create file handler which logs in a specific directory
logdir = '.'
if 'APP_LOG_DIR' in os.environ:
logdir = os.environ['APP_LOG_DIR']
logfile = datetime.now().strftime("run_%b_%d_%Y_%H_%M_%S.log")
fh = logging.FileHandler(os.path.join(logdir, logfile))
fh.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter('[%(asctime)s][%(name)s][%(levelname)s] %(message)s')
fh.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.debug("my first log line")
The log directory may be configured with the environment variable APP_LOG_DIR and path names are built in a platform independent way thanks to os.path.

how to make two log files using logging module in python?

Hello folks!
#this is values from xml file.
clientID = logObject['meta']['clientID']
authToken = logObject['meta']['authToken']
logType = logObject['logType']
FORMAT = '%(asctime)-15s %(logType)s %(process)d %(user)-8s %(message)s'
d = {'logType': logType ,'user': getpass.getuser()}
#line creating two log files(access.log and error.log)
logging.basicConfig(filename = 'access.log', filemode = 'w', format=FORMAT)
logging.basicConfig(filename = 'error.log', filemode = 'w', format=FORMAT)
if(clientID == ""):
# logger = setup_logger('first_logger', 'access.log',logType)
logger.warning('Please Enter clientID', extra=d)
This is my sample code.
what i need is to create two files which i have mentioned. but problem is it only creates single file everytime and messages goes to that file only.
So i want that if i mention logger.error("msg") or logger.warning("msg") then it should go to that log file.
When you use just logging you actually use single root logger created during import logging. You may use it with several handlers. For example:
# 1. logging to file
filename = (
'log_file{}.log'
.format(
dt.datetime.today().strftime("_date_%Y-%m-%d_time_%H-%M-%S")))
# path to log folder.
path = os.path.join(os.getcwd(), 'logs')
# create log folder if it does not exist.
if not os.path.isdir(path):
os.makedirs(path, exist_ok=True)
to_file = logging.FileHandler(os.path.join(path, filename))
to_file.addFilter(lambda x: x.levelno in [20, 40])
# 2. logging to console
to_console = logging.StreamHandler()
to_console.addFilter(lambda x: x.levelno in [20, 21, 40])
# 3. root logger configuration
logging.basicConfig(
level=10,
datefmt='%Y-%m-%d %H:%M:%S',
format='[%(asctime)s]:%(threadName)s:%(levelname)s:%(message)s',
handlers=[to_console, to_file])
If you want to log into 2 files then just create 2 handlers logging.FileHandler(...), register them, and use newly configured root logger as usual:
logging.info('Some info')
Another option is to create 2 loggers. Usually you need to do so if you want to separate several sources of log messages.

how to read different block setting from kinto.ini file

I created a different block in my kinto.ini file and i want to use those setting in my program.
#kinto.ini
[mysetting]
name = json
username = jsonmellow
password = *********
[app:main]
use = egg:kinto
kinto.storage_url = postgre//
if we use 'config.get_setting' function of kinto it gives me the setting of the default block "app:main" only. so how can i get the other setting from "mysetting" block.
you can use prefix for your settings like:
[app:main]
...
mysetting.name = json
mysetting.username = jsonmellow
But if you still need some extra section in ini: How can I access a custom section in a Pyramid .ini file?

Python3 logging module: set variable for logging levels in config

I am using the logging module in Python 3.5, I feel like I have done this before, but I would like like to change the logging level with a constant supplied by my config file.
Currently I am using:
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
logger.setLevel(logging.INFO)
I would like to declare INFO, DEBUG etc in the config file and replace using the literal.
my_script_config:
LOG_LEVEL = 'DEBUG' #this value to be modified by user.
script:
import my_script_conf as msc
SET_LEVEL = 'logging.' + msc.LOG_LEVEL
BASIC_LEVEL = 'level=' + SET_LEVEL
logger = logging.getLogger(__name__)
logging.basicConfig(BASIC_LEVEL)
logger.setLevel(SET_LEVEL)
Python gets upset by this, any help on my terrible coding would be much appreciated. Any route to achieve the same result, I basically want it readable and easy to use, to have the logging modules level set in the external config file seems like the most sensible option. You pythonista's may have other ideas!
Thanks, Frank
I concatenated the string in a another variable then ran an exec on the variable.
my_script_config:
LOG_LEVEL = 'DEBUG' #this value to be modified by user.
script:
import my_script_conf as msc
SET_LEVEL = 'logger.setLevel(' + msc.LOG_LEVEL + ')
BASIC_LEVEL = 'logging.basicConfig(level=' + msc.LOG_LEVEL + ')'
logger = logging.getLogger(__name__)
exec(SET_LEVEL)
exec(BASIC_LEVEL)

Resources