menu

Questions & Answers

Variable containing Log is receiving null

I am wanting to create a log that informs the registration of a new user, the problem is in the line, i'm wanting to create a log that informs the registration of a new user, the problem is in the line $log_message = Log::channel('logRegisterUser')->info("{$name_user} registered using the IP {$ip_user} in {$data}."); which when placed in a dd() is revealed as null. With this creating error at line 'description_log' => $log_message,.

  • logging.php
        'logRegisterUser' => [
            'driver' => 'single',
            'path' => storage_path('logs/logRegisterUser.log'),
            'level' => 'debug',
        ],
  • RegisteredUserController.php
    public function __construct()
    {
        $this->logModel = new LoggingModel();
    }
    
    public function create()
    {
        return view('auth.register');
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'confirmed', Rules\Password::defaults()],
        ]);

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        event(new Registered($user));

        $date = Carbon::now()->format('d/m/Y H:i:s');
        $name_user = $user->name;
        $ip_user = request()->ip();
        $log_message = Log::channel('logRegisterUser')->info("{$name_user} registered using the IP {$ip_user} in {$data}.");

        $this->logModel->create([
            'description_log' => $log_message,
            'relation' => '',
        ]);

        Auth::login($user);
        return redirect(RouteServiceProvider::HOME);
    }
Comments:
2023-01-18 23:30:08
Log::info() doesn't return a value. What are you expecting?
2023-01-18 23:30:08
I'm expecting "{$name_user} registered using the IP {$ip_user} in {$data}."
2023-01-18 23:30:09
I'd suggest assigning that line to a variable, then pass that variable into both Log::info() and into your logModel
Answers(1) :

Log->info() does not return a value. Instead write message in your variable first and then pass it to your Logger :

$log_message = "{$name_user} registered using the IP {$ip_user} in {$data}."; 
Log::channel('logRegisterUser')->info($log_message);