Uncategorized

How to Log Messages in Drupal 8

Drupal 8 tutorial

Note: For related topics to this tutorial that you may need to understand, you may be interested in tutorials about using dependency injection, which is needed to get the logger service into your code. Understand the Service Container and Get a Service from the Container are good places to start with that. Additionally the Entity API Hooks tutorial uses logging as an example when working with entities.

Developers familiar with Drupal 7 will also be familiar with watchdog(), an API function that allows you to create a log message that appears on the Reports page in the Drupal administrative UI. Implementing D7’s hook_watchdog allows module developers to customize the destination of these log messages. In Drupal 8, both functions are replaced by a PSR-3-compatible logging interface (change notice).

In this tutorial, you’ll learn how to create the D8 equivalent of D7’s watchdog() function: creating a log message that appears on the Reports administrative page. In a future tutorial, we’ll cover how to create your own logger, and use it in your custom module. However, I’ve left you some hints and pointers at the end of this tutorial if you care to dive in before then.

Before and after

There is a straight-up D8 procedural equivalent of D7’s watchdog function, as we learn from the change notice:

Drupal 7 watchdog()

<?php
// Logs a notice
watchdog('my_module', $message, array());
// Logs an error
watchdog('my_module', $message, array(), WATCHDOG_ERROR);
?>

Drupal 8 logger class

<?php
// Logs a notice
\Drupal::logger('my_module')->notice($message);
// Logs an error
\Drupal::logger('my_module')->error($message);
?>

Learn from Examples

I downloaded the Examples for Developers module (version 8.x-1.x, but please consult the project page for the latest version) and looked for an example of this in action. I found a good one in the content_entity_example module within the Examples project.

After downloading Examples, open the project in your code editor or IDE of choice. (With Drupal 8’s object-oriented framework, I highly recommend using an IDE such as PhpStorm, simply because it makes navigating classes, methods, and their implementations so much easier. If you’re new to using an IDE, we have a nice set of PhpStorm IDE how-to videos from the creators, JetBrains, available on Drupalize.Me.)

Let’s take a look at the ContactDeleteForm class in examples/content_entity_example.

In your code editor, navigate to:
examples/content_entity_example/src/Form/ContactDeleteForm.php

If you have Examples downloaded to a Drupal 8 installation, that path is:
DRUPALROOT/modules/examples/content_entity_example/src/Form/ContactDeleteForm.php

In the submitform() method, we see the following:

public function submitForm(array &$form, FormStateInterface $form_state) {
    $entity = $this->getEntity();
    $entity->delete();

    \Drupal::logger('content_entity_example')->notice('@type: deleted %title.',
        array(
            '@type' => $this->entity->bundle(),
            '%title' => $this->entity->label(),
        ));
    $form_state->setRedirect('entity.content_entity_example_contact.collection');
}

Let’s break this down.

Annotated logger

a. \Drupal::

In this example, the submitForm method relies on the Drupal service container wrapper, a class which smoothes the transition from procedural to object-oriented code in the Drupal code base. The double colon means that we’re accessing a class from “elsewhere,” or outside of the class that it’s called from. It’s kind of like using an absolute path instead of relative one.

View More 2501
View More 2502
View More 2503
View More 2504
View More 2505
View More 2506
View More 2507
View More 2508
View More 2509
View More 2510
View More 2511
View More 2512
View More 2513
View More 2514
View More 2515
View More 2516
View More 2517
View More 2518
View More 2519
View More 2520
View More 2521
View More 2522
View More 2523
View More 2524
View More 2525
View More 2526
View More 2527
View More 2528
View More 2529
View More 2530
View More 2531
View More 2532
View More 2533
View More 2534
View More 2535
View More 2536
View More 2537
View More 2538
View More 2539
View More 2540
View More 2541
View More 2542
View More 2543
View More 2544
View More 2545
View More 2546
View More 2547
View More 2548
View More 2549
View More 2550
View More 2551
View More 2552
View More 2553
View More 2554
View More 2555
View More 2556
View More 2557
View More 2558
View More 2559
View More 2560
View More 2561
View More 2562
View More 2563
View More 2564
View More 2565
View More 2566
View More 2567
View More 2568
View More 2569
View More 2570
View More 2571
View More 2572
View More 2573
View More 2574
View More 2575
View More 2576
View More 2577
View More 2578
View More 2579
View More 2580
View More 2581
View More 2582
View More 2583
View More 2584
View More 2585
View More 2586
View More 2587
View More 2588
View More 2589
View More 2590
View More 2591
View More 2592
View More 2593
View More 2594
View More 2595
View More 2596
View More 2597
View More 2598
View More 2599
View More 2600
View More 2601
View More 2602
View More 2603
View More 2604
View More 2605
View More 2606
View More 2607
View More 2608
View More 2609
View More 2610
View More 2611
View More 2612
View More 2613
View More 2614
View More 2615
View More 2616
View More 2617
View More 2618
View More 2619
View More 2620
View More 2621
View More 2622
View More 2623
View More 2624
View More 2625
View More 2626
View More 2627
View More 2628
View More 2629
View More 2630
View More 2631
View More 2632
View More 2633
View More 2634
View More 2635
View More 2636
View More 2637
View More 2638
View More 2639
View More 2640
View More 2641
View More 2642
View More 2643
View More 2644
View More 2645
View More 2646
View More 2647
View More 2648
View More 2649
View More 2650
View More 2651
View More 2652
View More 2653
View More 2654
View More 2655
View More 2656
View More 2657
View More 2658
View More 2659
View More 2660
View More 2661
View More 2662
View More 2663
View More 2664
View More 2665
View More 2666
View More 2667
View More 2668
View More 2669
View More 2670
View More 2671
View More 2672
View More 2673
View More 2674
View More 2675
View More 2676
View More 2677
View More 2678
View More 2679
View More 2680
View More 2681
View More 2682
View More 2683
View More 2684
View More 2685
View More 2686
View More 2687
View More 2688
View More 2689
View More 2690
View More 2691
View More 2692
View More 2693
View More 2694
View More 2695
View More 2696
View More 2697
View More 2698
View More 2699
View More 2700
View More 2701
View More 2702
View More 2703
View More 2704
View More 2705
View More 2706
View More 2707
View More 2708
View More 2709
View More 2710
View More 2711
View More 2712
View More 2713
View More 2714
View More 2715
View More 2716
View More 2717
View More 2718
View More 2719
View More 2720
View More 2721
View More 2722
View More 2723
View More 2724
View More 2725
View More 2726
View More 2727
View More 2728
View More 2729
View More 2730
View More 2731
View More 2732
View More 2733
View More 2734
View More 2735
View More 2736
View More 2737
View More 2738
View More 2739
View More 2740
View More 2741
View More 2742
View More 2743
View More 2744
View More 2745
View More 2746
View More 2747
View More 2748
View More 2749
View More 2750
View More 2751
View More 2752
View More 2753
View More 2754
View More 2755
View More 2756
View More 2757
View More 2758
View More 2759
View More 2760
View More 2761
View More 2762
View More 2763
View More 2764
View More 2765
View More 2766
View More 2767
View More 2768
View More 2769
View More 2770
View More 2771
View More 2772
View More 2773
View More 2774
View More 2775
View More 2776
View More 2777
View More 2778
View More 2779
View More 2780
View More 2781
View More 2782
View More 2783
View More 2784
View More 2785
View More 2786
View More 2787
View More 2788
View More 2789
View More 2790
View More 2791
View More 2792
View More 2793
View More 2794
View More 2795
View More 2796
View More 2797
View More 2798
View More 2799
View More 2800
View More 2801
View More 2802
View More 2803
View More 2804
View More 2805
View More 2806
View More 2807
View More 2808
View More 2809
View More 2810
View More 2811
View More 2812
View More 2813
View More 2814
View More 2815
View More 2816
View More 2817
View More 2818
View More 2819
View More 2820
View More 2821
View More 2822
View More 2823
View More 2824
View More 2825
View More 2826
View More 2827
View More 2828
View More 2829
View More 2830
View More 2831
View More 2832
View More 2833
View More 2834
View More 2835
View More 2836
View More 2837
View More 2838
View More 2839
View More 2840
View More 2841
View More 2842
View More 2843
View More 2844
View More 2845
View More 2846
View More 2847
View More 2848
View More 2849
View More 2850
View More 2851
View More 2852
View More 2853
View More 2854
View More 2855
View More 2856
View More 2857
View More 2858
View More 2859
View More 2860
View More 2861
View More 2862
View More 2863
View More 2864
View More 2865
View More 2866
View More 2867
View More 2868
View More 2869
View More 2870
View More 2871
View More 2872
View More 2873
View More 2874
View More 2875
View More 2876
View More 2877
View More 2878
View More 2879
View More 2880
View More 2881
View More 2882
View More 2883
View More 2884
View More 2885
View More 2886
View More 2887
View More 2888
View More 2889
View More 2890
View More 2891
View More 2892
View More 2893
View More 2894
View More 2895
View More 2896
View More 2897
View More 2898
View More 2899
View More 2900
View More 2901
View More 2902
View More 2903
View More 2904
View More 2905
View More 2906
View More 2907
View More 2908
View More 2909
View More 2910
View More 2911
View More 2912
View More 2913
View More 2914
View More 2915
View More 2916
View More 2917
View More 2918
View More 2919
View More 2920
View More 2921
View More 2922
View More 2923
View More 2924
View More 2925
View More 2926
View More 2927
View More 2928
View More 2929
View More 2930
View More 2931
View More 2932
View More 2933
View More 2934
View More 2935
View More 2936
View More 2937
View More 2938
View More 2939
View More 2940
View More 2941
View More 2942
View More 2943
View More 2944
View More 2945
View More 2946
View More 2947
View More 2948
View More 2949
View More 2950
View More 2951
View More 2952
View More 2953
View More 2954
View More 2955
View More 2956
View More 2957
View More 2958
View More 2959
View More 2960
View More 2961
View More 2962
View More 2963
View More 2964
View More 2965
View More 2966
View More 2967
View More 2968
View More 2969
View More 2970
View More 2971
View More 2972
View More 2973
View More 2974
View More 2975
View More 2976
View More 2977
View More 2978
View More 2979
View More 2980
View More 2981
View More 2982
View More 2983
View More 2984
View More 2985
View More 2986
View More 2987
View More 2988
View More 2989
View More 2990
View More 2991
View More 2992
View More 2993
View More 2994
View More 2995
View More 2996
View More 2997
View More 2998
View More 2999
View More 3000

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button