Skip to content

Allow formatter customizations - #18

Merged
yyuu merged 8 commits into
fluent:masterfrom
EvaSDK:allow-formatter-customizations
Jun 3, 2015
Merged

Allow formatter customizations#18
yyuu merged 8 commits into
fluent:masterfrom
EvaSDK:allow-formatter-customizations

Conversation

@EvaSDK

Copy link
Copy Markdown
Contributor

I am currently pursuing the goal of using fluent just like any other logging Handler/Formatter via logging.config

In order to do that I had to solve a couple of problems that make default FluentRecordFormatter not easy to deal with. It is now able to take a format argument which is a dictionary that holds keys and values. Values are regular LogRecord standard Python %-style mapping keys. The defaults are unchanged.

I also added a couple of fixes I found in other pull requests as I found them as well during my tests, so this should cover Issue #7, PR #8 and PR #16.

Finally I added a sphinx ready docstring to FluentRecordFormatter._structuring although this function could probably be merged in format directly since I don't see it used anywhere else.

Once merged, the following sample program:

import logging
from fluent.handler import FluentHandler, FluentRecordFormatter
logger = logging.getLogger('toto')
logger.setLevel(logging.DEBUG)
handler = FluentHandler('hello.test')
handler.setLevel(logging.DEBUG)
formatter = FluentRecordFormatter(fmt={
'msg': '%(message)s',
'level': '%(levelname)s',
'hostname': '%(hostname)s',
})
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info('hel""lo')

logs

2014-08-01 13:59:05 +0200 hello.test: {"msg":"hel\"\"lo","message":"hel\"\"lo","hostname":"seta.local","level":"INFO"}

to fluentd.

@kdeldycke

Copy link
Copy Markdown

👍

@EvaSDK

Copy link
Copy Markdown
ContributorAuthor

A sample configuration would be:

logging:
version: 1formatters:
brief:
format: '%(message)s'default:
format: '%(asctime)s %(levelname)-8s %(name)-15s %(message)s'datefmt: '%Y-%m-%d %H:%M:%S'fluent_fmt:
'()': fluent.handler.FluentRecordFormatterformat:
message: '%(message)s'level: '%(levelname)s'hostname: '%(hostname)s'where: '%(module)s.%(funcName)s'handlers:
console:
class : logging.StreamHandlerlevel: DEBUGformatter: defaultstream: ext://sys.stdoutfluent:
class: fluent.handler.FluentHandlerhost: localhostport: 24224tag: test.loggingformatter: fluent_fmtlevel: DEBUGnull:
class: logging.NullHandlerloggers:
amqp:
handlers: [null]propagate: Falseconf:
handlers: [null]propagate: False'': # root loggerhandlers: [console, fluent]level: DEBUGpropagate: False

loaded likeso:

importlogging.configimportyamlwithopen('logging.yaml') asfd:
conf=yaml.load(fd)
logging.config.dictConfig(conf['logging'])

the way to customize formatter via logging.config.dictConfig drove me a bit crazy until I found the proper documentation. So I figured it would be useful to add it as an example here.

@EvaSDKEvaSDK mentioned this pull request Aug 5, 2014
@EvaSDK
EvaSDKforce-pushed the allow-formatter-customizations branch from 5082d68 to fbd05d8CompareFebruary 3, 2015 14:49
@EvaSDK

Copy link
Copy Markdown
ContributorAuthor

Branch updated against 0.3.5.

@kiyoto

Copy link
Copy Markdown
Contributor

@EvaSDK

The idea looks good to me. Do you think you can add a test for the string input case? After that it's ready to be merged.

@EvaSDK

Copy link
Copy Markdown
ContributorAuthor

I can look into it yes.

@EvaSDK

Copy link
Copy Markdown
ContributorAuthor

Added some tests and fixed a few issues while doing so.

The most annoying thing to me is the drop of python 2.6 support due to class changes in python logging module and how it handles the asctime attribute. I think dropping 2.6 support is not too bad anyway due to it being unsupported on most distributions nowadays. If you still want to support it, I can elaborate an alternative solution.

@EvaSDK
EvaSDKforce-pushed the allow-formatter-customizations branch from 2768952 to 70d68cbCompareMay 27, 2015 13:36
@EvaSDK

Copy link
Copy Markdown
ContributorAuthor

Added py26 compatibility back. It does not look pretty but if you want it for older distributions, you have it.
I included README update from PR #22.

This fixes: issue #7, PR #8, PR #22 / issue #23

With PR #31, this adds up to 3 pull requests to fix conformance to logging classes. Please tell me what to do with this PR, it has been almost a year and I did everything I could to satisfy your modification requests.

@repeatedly

Copy link
Copy Markdown
Member

@yyuu Could you check this PR?
I confirmed this change resolves #34 issue.
We should fix issue 34 to avoid logging trouble.

@yyuu

yyuu commented May 29, 2015

Copy link
Copy Markdown
Member

Looks good to me.

@repeatedly

Copy link
Copy Markdown
Member

@yyuu Feel free to merge this PR and release new version. Maybe update minor version is better?
After that, we will close the issue on fluentd repository.

@EvaSDK

Copy link
Copy Markdown
ContributorAuthor

The PR contains a version bump to 0.4.0 as I felt this was an important enough change.

@repeatedly

Copy link
Copy Markdown
Member

How about this PR? > #31

@yyuu

yyuu commented Jun 1, 2015

Copy link
Copy Markdown
Member

@repeatedly I don't have permission to upload fluent-logger to PyPI....

@EvaSDK

Copy link
Copy Markdown
ContributorAuthor

#31 squashed all my commits in one and adds a feature that is debatable on its own. Imho, it should be reworked as a single commit on top of my branch first.

Comment thread.travis.yml

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EvaSDK Why did you remove 2.6 from .travis.yml? It's still in tox.ini and seems working fine at least for me.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like I forgot to update .travis.yml when I re-added support for it. I'll fix that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it. I'll fix it by myself.

@yyuu
yyuu merged commit 70d68cb into fluent:masterJun 3, 2015
yyuu added a commit that referenced this pull request Jun 3, 2015
@EvaSDK

Copy link
Copy Markdown
ContributorAuthor

w00t, thanks.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@EvaSDK@kdeldycke@kiyoto@repeatedly@yyuu@quasipedia