Home » Posts tagged 'Framework'
Tag Archives: Framework
We often have to use different micro-services who write in many log files. Use utilities like Kibana is a good thing, but in order to take full advantage of its features we have to try to standardize and normalize the logs.
The company where I work having introduced Kibana recently, he asked me to implement a proper strategy to log all the micro-services.
First we defined the basic rules of the logs.
General logging rules of team
- Date + Hours : 2017-02-09 10:24:00
- Log Level : TRACE DEBUG INFO WARN ERROR FATAL
- Customer Id
- Site or ProductLine
If log is ERROR mandatory fields are also:
Describing the exact point of the code that has launched the exception.
- Start and End of service call
- External call
- Key points of the application
- 1 file for service
- 1 error file for service
Monolog sends your logs to files, sockets, inboxes, databases and various web services. See the complete list of handlers below. Special handlers allow you to build advanced logging strategies.
This library implements the PSR-3 interface that you can type-hint against in your own libraries to keep a maximum of interoperability. You can also use it in your applications to make sure you can always use another compatible logger at a later time. As of 1.11.0 Monolog public APIs will also accept PSR-3 log levels. Internally Monolog still uses its own level scheme since it predates PSR-3. Reference:https://github.com/Seldaek/monolog
Working with Symfony and Monolog
Symfony comes with native Monlog library that allows you to create logs that can be stored in a variety of different places.
The logger service has different methods for different logging levels/priorities. You can configure the logger to do different things based on the level of a message (e.g. send an email when an error occurs).
The logger has a stack of handlers, and each can be used to write the log entries to different locations (e.g. files, database, Slack, etc).
For a proper and complete vision of Monolog in Symfony, refer to the official documentation: http://symfony.com/doc/current/logging.htmlhttp://symfony.com/doc/current/logging/processors.html http://symfony.com/doc/current/reference/configuration/monolog.html
We use as example one of many micro-services that my company use: “serviceactivator” web service.
What it does the micro-service does not matter. What interests us is how it write logs.
1) Configuration of monolog
2) Base Logger: the parent logger class
3) The specific logger class
4) Configuration of services
5) Creation of Monolog Processor
Monolog allows you to process the record before logging it to add some extra data. A processor can be applied for the whole handler stack or only for a specific handler.
A processor is simply a callable receiving the record as its first argument. Processors are configured using the monolog.processor DIC tag. See the reference about it.
This processor, for example, is responsible to place in extra all key/value pairs passed in the context.
6) Declare service Processor
The method __invoke of Processor will be called every times that ServiceActivatorLogger write log.
7) Call log Example
8) Specific Processor for log error
We will use IntrospectionProcessor that will be called only if application want log a error:
9) Separate file for error log
Nothing could be simpler with monolog:
10) Custom Formatter
So far everything is fine, but what happens if we have not ‘customerId’ or productLine in context array? We will have a log like this:
You can see that brackets that should contain customerId and productLine are empty.
So, the last step, act to printing well logs are customize monolog formatter class. Specifically we will customize LineFormatter class.
If customerId and productLine are empty the application use default format (SIMPLE_FORMAT) and we will have a log like this:
So now, the formatter service can change in this way:
We can do this because we put the custom logic, act to handle format of log, inside custom format method of formatter class.
The next development could be use configuration files to tell to processor which fields include in extra field of record structure:
And pass it to service:
PHP is one of the most popular programming languages around the world, and the recent PHP 7 release made this server-side programming language better and more stable than ever.
PHP is widely used in major projects. Facebook, for example, utilizes PHP for maintaining and creating their internal systems. WordPress uses PHP to power its internals, which in return is powering more than 26% of the web. Currently, PHP powers more than 82% of websites (whose server-side programming languages the Web Technology Surveys site is able to track).
In this article, we’ll look at three of the most popular PHP frameworks: Symfony, Laravel, and Yii. We’ll examine how they compare to help you decide which one might be the best fit for your needs.
Why pick a PHP framework?
What’s the point of using a framework instead of raw PHP to develop your application? A few benefits of using a framework include:
- A PHP framework makes development faster. For example, you don’t have to write complex queries to retrieve the data from the database. PHP frameworks provide CRUD operations (Create, Read, Update, and Delete).
- Frameworks enable developers to scale systems easily.
- Code maintenance is easier than with a vanilla PHP application. The application code is concise and easy to work with.
- The MVC model ensures rapid development.
- Frameworks are better in securing the web application from common security threats.
- The don’t repeat yourself (DRY) principle ensures that minimal code has maximum impact.
The above benefits are too significant to be ignored. Even though raw PHP can be used to create any application, the current development standards require tools and time-management skills to meet the market demand.
How to choose a PHP framework
Answering a few questions can help you choose a framework:
- What are the features and functionality of the framework? (Does it offer what I need?)
- What is the learning curve of the framework?
- How scalable is the framework?
- Is the framework actively developed and maintained by the core team?
- Does the framework provide long-term support (LTS support) ?
- Does the framework have a strong community support?