This question may have been asked before. I have searched for answers, but I haven't found what I was looking for.
In Symfony 2.3, is there a way to disable the logger for specific requests? I mean, I am using a SOAP service for my project. When I send a request to login, the username and password are dumped straight as plain text into the log file. Is there a way to stop logging this kind of specific requests?
For example, when I send a request for login, the logger should be disabled, but for all other request it works again. Is this possible?
depending if your are in Prod or Dev environement but everything is in config.yml or config_dev.yml :
to disable logging just remove monolog configuration like this :
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
bubble: false
# uncomment to get logging in your browser
# you may have to allow bigger header sizes in your Web server configuration
#firephp:
# type: firephp
# level: info
#chromephp:
# type: chromephp
# level: info
but in my opinion , you shouln't do this because logging allows you to improve significantly your code !
Logging except for a specific service :
You need to create a specific log channel for your service as described there :
http://symfony.com/doc/current/cookbook/logging/channels_handlers.html
and there :
http://symfony.com/doc/current/reference/dic_tags.html#dic-tags-monolog
you ll be able to separate your soap log from others and eventually send it to null
Related
I am using Remote URL option which reaches out to remote web server to retrieve needed data. Simply using this,
https://rundeck:test#myserver.com works. However, I would like to pass the password in secure way so...
Option 1 uses 'Secure pass input' and pass is retrieved from key storage, however the password is then not added to the remote URL in
Option 2, which uses Remote URL, https://rundeck:${option.password.value}#myserver.com. My remote servers receives the password as ${option.password.value} and not the actual password value retrieved in Option 1. I understand that Secure Remote Authentication can't be used in Options, however i don't believe I have seen restrictions on what I want to do with Secure † Password input in Rundeck's docs.
Lastly, typing in the password in Secure † Password input option does add the password to the mentioned URL above. I have tested and verified that ${option.password.value} value can be passed in a job's step, that part works. However, it does not appear to work in cascading options.
Currently, secure options values are not expanded as a part of remote options, you can suggest it here (similar to this). Alternatively, you can create a specific custom plugin for that.
Another approach is to design a workflow that uses the HTTP Workflow Step Plugin (passing your secure password as a part of the authentication in the URL) to access the web service + JQ Filter to generate the desired data, then in another/step you can get that data using data variables.
Like this:
- defaultTab: nodes
description: ''
executionEnabled: true
id: 7f34f7ff-c4a3-4616-a2aa-0df491450366
loglevel: INFO
name: HTTP
nodeFilterEditable: false
options:
- name: mypass
secure: true
storagePath: keys/password
valueExposed: true
plugins:
ExecutionLifecycle: null
scheduleEnabled: true
sequence:
commands:
- configuration:
authentication: None
checkResponseCode: 'false'
method: GET
printResponse: 'true'
printResponseToFile: 'false'
proxySettings: 'false'
remoteUrl: https://user:${option.mypass}#myserver.com
sslVerify: 'true'
timeout: '30000'
nodeStep: false
plugins:
LogFilter:
- config:
filter: .
logData: 'true'
prefix: result
type: json-mapper
type: edu.ohio.ais.rundeck.HttpWorkflowStepPlugin
- exec: echo "name 2 is ${data.Name2}"
keepgoing: false
strategy: node-first
uuid: 7f34f7ff-c4a3-4616-a2aa-0df491450366
Do you have an example configuration to send logs by email with the Mailer component of Symfony 5.1.
In the Symfony blog we announce this feature, but I can't put the right config in monolog.yaml
https://symfony.com/blog/new-in-symfony-5-1-misc-improvements-part-3 :
That's why in Symfony 5.1 we added a new Monolog log handler which uses the Mailer component to send the logs via email.
Unfortunately this addition only covers the actuall MailerHandler class in the monolog-bridge. This does not cover the possibility to configure it in the monolog-bundle (that's the drawback if those components are distributed over multiple packages).
The PR for the change in the monolog-bundle is still open, and can be found here: Add Symfony Mailer support #354.
If you don't want to wait for the change in the monolog-bundle you could already use it by defining the handler as a service and then using it with the service type in the monolog configuration.
So define your service:
services:
# this configures the "mail" as a prototype, so you can
# define the sender and recipient mail addresses here
symfony_mailer_service_template:
class: Symfony\Component\Mime\Email
calls:
- ['from', ['webapp#example.com']]
- ['to', ['ops#example.com']]
- ['subject', ['Logs']]
symfony_mailer_service:
class: Symfony\Bridge\Monolog\Handler\MailerHandler
arguments:
- '#mailer.mailer'
- '#symfony_mailer_service_template'
- !php/const Monolog\Logger::DEBUG # log level
- true # bubble
And then in your mailer configuration you could use it like this:
monolog:
handlers:
main:
type: fingers_crossed
handler: deduplicated
deduplicated:
type: deduplication
handler: symfony_mailer
symfony_mailer:
type: service
id: symfony_mailer_service
I want to log messages to separate files depending on the context. Currently everything is going into the environment's log file. How can this be accomplished with Monolog in Symfony2?
In your config files (config.yml or config_dev.yml and config_prod.yml) under monolog: register new handlers for each file you want to log to.
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
the environment's log file will receive everything from debug level up, but the main logger service will only log with action level error and up.
Add your own handlers for the files you want to log to in your config files.
cron_logger:
type: stream
path: "%kernel.logs_dir%/cron.log"
level: notice
channels: "cronchan"
You can choose which level of messages will be logged in the file. You should specify which channel will log to this handler. In this case the channel name chosen was "cronchan". At the same level as handlers, register your new channel (you need MonologBundle 2.4 for this).
monolog:
handlers:
...
cron_logger: ...
channels: ["cronchan"]
You can now log specifically to the cron.log file by getting the correct channel's service in a controller: $logger = $this->get('monolog.logger.cronchan');
Now everything from notice level and up will be logged in the cron.log file. The messages will possibly also be logged in the environment's log file, but you can remove the channel from the file's handler's channels.
I would like to authenticate the user through an API (external user provider) running my integration tests.
my config_test.yml
imports:
- { resource: config.yml }
- { resource: parameters_test.yml }
framework:
test: ~
session:
storage_id: session.storage.mock_file
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: info
After the login (SUCCEED) the next step is ask for user information and I got:
"Full authentication is required to access this resource."
I guess something is happening with the stored session.
I am using Redis on dev and prod to store sessions.
Mocking sessions on the test environment because of
"Failed to start the session because headers have already been sent by "/path/to/bin/phpunit" at line 2."
Doing it manually is working like a charm.
session.storage.mock_file uses by default %kernel.cache_dir%/sessions for a storing of session's file. Check if this folder is writable for both server user and cli user.
'Setting up Permissions' http://symfony.com/doc/current/book/installation.html#configuration-and-setup
Another 2 possible reasons of error:
stateless: true on your firewall in firewalls section. The ContextListener is not created and Token is not saved to the session. AnonymousToken is assigned to next request.
stateless: true you also can have RememberMeToken instead of AnonymousToken if remember_me feature enabled. This token is also not full fledged.
UPDATE
Ensure that intercept_redirects is false in config_test.yml as it may break all redirects.
web_profiler:
toolbar: false
intercept_redirects: false
Getting this exception in Symfony 2.5.5 with Swiftmailer 5.3.0. I'm following the cookbook example exactly. The error is thrown when calling MessageDataCollector#getMessages():
// Check that an e-mail was sent
$this->assertEquals(1, $mailCollector->getMessageCount());
$collectedMessages = $mailCollector->getMessages();
$message = $collectedMessages[0];
The message count assertion is also failing with a value of zero.
As far as I can tell the collector is failing to do any actual collecting in the action. Any ideas?
I encountered the same problem after I applied kriswallsmith's test optimization trick. I could see the result of the mail sending in the web profiler when running the development version, but couldn't get the data in the testing environment.
After applying Kris' trick I noticed that the swiftmailer.mailer.default.plugin.messagelogger service was not registered with the container during the test, so the collect() method of the MessageDataCollector class did not log the data for the mail sending. This is why it wasn't possible to retrieve information from the swiftmailer collector.
The solution is either not to overrride the initializeContainer() method in AppKernel.php or to override it, but making sure messagelogger service is available for test cases that send mails.
#codeBetyar is right - problem occurs when swiftmailer.mailer.default.plugin.messagelogger is not registered in container.
But instead of getting rid of the optimization trick - you just need to update swiftmailer configuration for test environment
swiftmailer:
logging: true
The thing is logging config value by default equals to kernel.debug (https://github.com/symfony/swiftmailer-bundle/blob/master/DependencyInjection/Configuration.php#L121) which (thanks to the trick) is true for the first test request, and false - for all following requests.
Above config forces logger being registered.
In my case I had multiple mailers in the config.yml file and the same problem was due to a missing parameter in the getMessages() call.
My config.yml file:
swiftmailer:
default_mailer: default_mailer
mailers:
default_mailer:
# some configuration
another_mailer:
# some configuration
The correct call was:
$mailCollector = $this->client->getProfile()->getCollector('swiftmailer');
$collectedMessages = $mailCollector->getMessages('default_mailer'); // please note the 'default_mailer' argument
$messagesCount = $mailCollector->getMessageCount('default_mailer')
My problem was that apparently if there are no messages to retrieve (or perhaps no swiftmailer instantiation in the code being tested?), the call to getMessages() returns this obtuse error. So I bypassed the problem by testing for getMessageCount() first.